Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在asp.net中设置配置文件属性_Asp.net_Profile - Fatal编程技术网

在asp.net中设置配置文件属性

在asp.net中设置配置文件属性,asp.net,profile,Asp.net,Profile,我已经创建了自己的注册表单,并使用Membership类创建了用户 MembershipCreateStatus status; MembershipUser newUser = Membership.CreateUser(tbxUsername.Text, tbxPassword.Text, tbxEma

我已经创建了自己的注册表单,并使用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;
}
特别感谢您的代码