Solved

ASP.NET Membership and Profile Set Up

Posted on 2010-08-24
12
658 Views
Last Modified: 2012-05-10
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
Comment
Question by:JT_SIRO
  • 7
  • 5
12 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33514390

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
 
LVL 16

Expert Comment

by:kris_per
ID: 33514444

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
 

Author Comment

by:JT_SIRO
ID: 33514878
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
 
LVL 16

Expert Comment

by:kris_per
ID: 33514992

A correction; it should be:

ProfileBase p = ProfileBase.Create("username");
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33515271

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

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

0
 

Author Comment

by:JT_SIRO
ID: 33516879
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Accepted Solution

by:
kris_per earned 250 total points
ID: 33520410
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
 

Author Comment

by:JT_SIRO
ID: 33525861
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
 
LVL 16

Expert Comment

by:kris_per
ID: 33526788

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
 

Author Comment

by:JT_SIRO
ID: 33527265
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
 

Author Comment

by:JT_SIRO
ID: 33535154
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
 
LVL 16

Expert Comment

by:kris_per
ID: 33537257

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now