Solved

ASP.NET Membership and Profile Set Up

Posted on 2010-08-24
12
663 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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