1

我创建了自己的注册表单并使用 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();

但是我收到以下异常消息

不能为匿名用户设置此属性。

任何想法为什么我得到这个?

4

2 回答 2

2

我认为这是因为您没有先获取配置文件(来自 DB/无论您使用什么)。

您的代码可能如下所示:

ProfileCommon p = Profile.GetProfile(tbxUsername.Text);
p.CountryCode = ddlCountry.SelectedValue;
p.DisplayName = tbxDisplayName.Text;
p.Save();
于 2009-06-21T10:32:34.610 回答
0

我遇到了同样的错误,这是因为我只设置了 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;
}

特别感谢这篇文章的代码!

于 2016-01-12T20:38:38.013 回答