When finished create user using (CreateUserWizard control) add profile information in the same process

StackOverflow https://stackoverflow.com/questions/17798263

  •  03-06-2022
  •  | 
  •  

When I try this code it's give this error message

This property cannot be set for anonymous users.

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    Roles.AddUserToRole((sender as CreateUserWizard).UserName, "Admin");

    Control ctrl = CreateUserWizard1.CreateUserStep.ContentTemplateContainer;
    TextBox txtAdminAddress= (TextBox)ctrl.FindControl("txtAdminAddress");
    TextBox txtAdminCountry= (TextBox)ctrl.FindControl("txtAdminCountry");
    TextBox txtAdminCity= (TextBox)ctrl.FindControl("txtAdminCity");

    HttpContext.Current.Profile.GetProfileGroup("AdminGroup").SetPropertyValue("AdminAddress", txtAdminAddress.Text);
    HttpContext.Current.Profile.GetProfileGroup("AdminGroup").SetPropertyValue("AdminCountry", txtAdminCountry.Text);
    HttpContext.Current.Profile.GetProfileGroup("AdminGroup").SetPropertyValue("AdminCity", txtAdminCity.Text);
    HttpContext.Current.Profile.Save();

}

Config:

<profile defaultProvider="AspNetSqlProfileProvider"> 
  <properties> 
    <group name="AdminGroup"> 
      <add name="AdminAddress" type="System.String"/> 
      <add name="AdminCountry" type="System.String"/> 
      <add name="AdminCity" type="System.String"/> 
    </group> 
  </properties> 
  <providers> 
     <clear/> 
     <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="MyConnectionString" applicationName="/"/> 
  </providers> 
</profile>
有帮助吗?

解决方案

After creating the User, You need to fetch the profile of recently created user to start updating values. Since No profile is Loaded, it won't allow to set these values for anonymous users.

string strUsername = (sender as CreateUserWizard).UserName;
ProfileCommon p = Profile.GetProfile(strUsername);

//update the field and save
p.AdminAddress= txtAdminAddress.Text;
p.Save();

The ProfileBase object (provided by the Page.Profile property) includes a useful GetProfile() function that retrieves, by user name, the profile information for a specific user.

GetProfile() returns a ProfileCommon object.

[ Note: The profile properties set in Config file doesn't allow setting values for Anonymous users. If you want to allow this for anonymous users also use:

<add name="AdminAddress" type="System.String" allowAnonymous="true"/>

]

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top