?
Solved

ASP.NET Membership and Profile Set Up

Posted on 2010-08-24
12
Medium Priority
?
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

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

Accepted Solution

by:
kris_per earned 1000 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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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