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