質問

独自のサインアップフォームを作成し、Membershipクラスを使用してユーザーを作成しました。

MembershipCreateStatus status;
MembershipUser newUser = Membership.CreateUser(tbxUsername.Text, 
                                               tbxPassword.Text, 
                                               tbxEmail.Text, 
                                               null, null, true, out status);

についてのコードを使用してユーザーが作成された後、そのようにいくつかのプロファイルプロパティを設定しようとします

Profile.CountryCode = ddlCountry.SelectedValue;
Profile.DisplayName = tbxDisplayName.Text;
Profile.Save();

ただし、次の例外メッセージが表示されます

このプロパティは匿名ユーザーには設定できません。

なぜこれを取得するのですか?

役に立ちましたか?

解決

これは、最初に(DB /使用しているものから)プロファイルをフェッチしなかったためだと思います。

コードは次のようになります。

ProfileCommon p = Profile.GetProfile(tbxUsername.Text);
p.CountryCode = ddlCountry.SelectedValue;
p.DisplayName = tbxDisplayName.Text;
p.Save();

他のヒント

同じエラーが発生しました。これは、authCookieのみを設定し、httpcontextユーザーを設定していないためです。見よ:

HttpCookie authCookie = HttpContext.CurrentRequest.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
    //Extract the forms authentication cookie
    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

    // If caching roles in userData field then extract
    string[] roles = authTicket.UserData.Split(new char[] { '|' });

    // Create the IIdentity instance
    IIdentity id = new FormsIdentity(authTicket);

    // Create the IPrinciple instance
    IPrincipal principal = new GenericPrincipal(id, roles);

    // Set the context user 
    HttpContext.Current.User = principal;
}

に感謝します。コードのこの投稿

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top