[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

ASP.NET Membership and Profile Set Up

I'm setting up a membership creation page for my .NET app.  I'm able to create a basic user just fine, but want to add a few custom fields to the users profile.  How do I populate profile values for another user?  See code below - I create a MembershipUser object from a CreateUserWizard. BUT I need to add a few profile value right after I create the user.  I can't seem to find the syntax.  Basically, I want to:

Update the "PhoneNumber" profile field for the user usr, that I just created.  I've created the PhoneNumber property in web.config already.

If I use Profile.PhoneNumber, that will apply to Me, as the logged in user.  I don't want that.  I want it to be applied to the profile of the user I am creating.



if (Membership.GetUser() != null)
			{
				MembershipUser usr = Membership.GetUser(CreateUserWizard1.UserName);
				usr.UnlockUser();
				usr.IsApproved = true;
                
				Membership.UpdateUser(usr);
               
                // Update profile info - how???


				CreateUserWizard1.ContinueDestinationPageUrl = "~/Admin_Pages/Administrator/UserAdministration.aspx";
			}
			else
			{
				CreateUserWizard1.ContinueDestinationPageUrl = "~/Admin_Pages/Administrator/UserAdministration.aspx";
			}

Open in new window

0
JT_SIRO
Asked:
JT_SIRO
  • 7
  • 5
1 Solution
 
kris_perCommented:

You can get the profile of an user by using the static 'Create' method of the ProfileBase class..

Profile p = ProfileBase.Create("username");

ProfileCommon profile = p as ProfileCommon;

profle.PhoneNumber = textPhoneNumber.Text;

profle.Save();
0
 
kris_perCommented:

Alternativly (without using ProfileCommon ):

Membership.UpdateUser(usr);
               
// Update profile info - how???

Profile p = ProfileBase.Create("username");

p.SetPropertyValue("PhoneNumber", textPhoneNumber.Text);

p.Save();

CreateUserWizard1.ContinueDestinationPageUrl = "~/Admin_Pages/Administrator/UserAdministration.aspx";
0
 
JT_SIROAuthor Commented:
I get this error when I try your example:
'CreateNewUser.Profile' is a 'property' but is used like a 'type' on this line:

Profile p = ProfileBase.Create("username");
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kris_perCommented:

A correction; it should be:

ProfileBase p = ProfileBase.Create("username");
0
 
kris_perCommented:

i hope you would use CreateUserWizard1.UserName for the user name as in:

ProfileBase p = ProfileBase.Create(CreateUserWizard1.UserName);

0
 
JT_SIROAuthor Commented:
I got it working well now.  Though, now I'm having trouble updating.  I have an editable grid of all my users.  My datasource has an update function called UpdateUser that takes a parameter of MembershipUtil, which is my custom membership class.  The function saves to the MembershipUser object just fine, but it doesn't account for my Profile fields.  I don't know how to pass the form values to my function, so I can update the Profile.

I tried adding parameters to my UpdateUser function, but it wouldn't allow it.  

I can't explain this very well.  But please let me know if you understand my problem and how to remedy it.  Thanks- (my code is of my datasource object and the function in my custom class that performs the save.  I commented out the updates that I tried to get to work, but failed.
<!-- Datasource in aspx page -->	
	<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="MembershipUtil"
		DeleteMethod="DeleteUser" SelectMethod="GetMembershipUtil" TypeName="MembershipUtil" 
		UpdateMethod="UpdateUser">        	
	</asp:ObjectDataSource>



    public static void UpdateUser(MembershipUtil MembershipUtil)
	{
		//Update User
		MembershipUser nUser = Membership.GetUser(MembershipUtil.Username);
		nUser.Email = MembershipUtil.EmailAddress;
		nUser.IsApproved = MembershipUtil.IsApproved;
		Membership.UpdateUser(nUser);

        ProfileBase p = ProfileBase.Create(MembershipUtil.Username);
        //ProfileBase p = ProfileBase.Create(MembershipUtil.Username);
        //p.SetPropertyValue("Initials", Initials);
        //p.SetPropertyValue("Nickname", Nickname);
        //p.SetPropertyValue("Phone", Phone);
        //p.SetPropertyValue("PRO", PRO);
        //p.Save();
		//Update Role
		//Removing the role in order to add a new one.
		string[] userRoles = Roles.GetRolesForUser(nUser.UserName);
		foreach (string userRole in userRoles)
		{
			Roles.RemoveUserFromRole(nUser.UserName, userRole);
		}

Open in new window

0
 
kris_perCommented:
I think I understand the issue. See if the below would fit.

If you want to (or currently) display the Profile fields also in the grid along with the membershipuser fields, then:      

- you can add profile fields as property members to MembershipUtil class and      

- update Grid to show profile fields as well. This will take care of loading/updating MembershipUtil object with both user and profile fields.      

- In GetMembershipUtil method, get profile details also from store and transfer them to MembershipUtil object properties.      
      To load profile from store, Create method itselft can be used like:
      ProfileBase p = ProfileBase.Create(MembershipUtil.Username);
      // use p.GetPropertyValue method to read values from profile object and then transfer them to MembershipUtil object.

- In UpdateUser method, when updating for user details, Save profile details also      
      To save profile:
      ProfileBase p = ProfileBase.Create(MembershipUtil.Username);
      // use p.SetPropertyValue method to set values in profile object
      p.Save();

Make sure all the profile properties are added in web.config as well.      
0
 
JT_SIROAuthor Commented:
Thanks kris_per.  That's exactly what I did, then I saw your post.  It works!

It seems a little awkward to pack membership and profile fields into my MembershipUtil, but it accomplishes the front end experience I want.  Is this normally broken into two Wizard steps or something?  e.g., create username, password - then enter user info on another page that feeds the profile object?

- Justin

0
 
kris_perCommented:

Glad that you are getting it working....As you mention, Yes. What you think is right. I guess this page is to provide 'Manage Users' functionality (for admin). Typically in this page users list can be displayed in a grid with some basic columns like (name, email, islocked, isapproved) and last column with 'Edit User' link/button for each user row. When this button link is clicked, it will open another 'Edit User' page where you can display membership fields with 'Profile (Preferences)' fields in the same page (or in the next page). A final Update button will save the member and profile details...

Since Grid here is used only to display, it will not do the Update tasks...Also you can use the MembershipCollection type returned by Membership.GetAllUsers directly as Datasource for the Grid...this will avoid the usage of interim MembershipUtil class....Edit User page can get the values from fields and update the Membership details and profile details...so it is possible to avoid the usage of MembershipUtil class there as well...
0
 
JT_SIROAuthor Commented:
Great, thanks for the help!  One other question...  I tried to deploy these pages to my Test environment on our production server and it's bombing on the profile properties when I put them in the web.config, as follows:
            <profile>
                  <properties>
                        <add name="Nickname" defaultValue="default"/>
                        <add name="Initials" defaultValue="default"/>
                        <add name="PRO" defaultValue="default"/>
                        <add name="Phone" defaultValue="default"/>
                  </properties>
            </profile>

I get this error:
This application was precompiled with personalization turned off, but it appears to have been turned on after the precompilation, which is not supported.

I guess I don't understand how to turn personalization on.  I did some research and tried adding this to my web.config:
<webParts>
                  <personalization defaultProvider="MyAspNetSqlPersonalizationProvider">
                        <providers>
                              <add connectionStringName="LocalSqlServer" name="MyAspNetSqlPersonalizationProvider" type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
                        </providers>
                  </personalization>
            </webParts>

It didn't work.  I get the error on all pages in my app, so I don't understand what I need to recompile.  Lemme know if you see the solution.  Thanks a ton!

Justin
0
 
JT_SIROAuthor Commented:
I've got it working now.  I forgot to move my app code dll to the server.  dumb dumb.
Thanks for all your help kris!
0
 
kris_perCommented:

Glad that its working fine and happy I could help...
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now