How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of code
page 3 of 7
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42038/ 103

<asp:createuserwizard> wizard step 1: gathering user-account data

Figure 2

The <asp:createuserwizard> control handles gathering up the user-account, email, password, and password recovery/answer data and then calling into the ASP.NET 2.0 membership system to register the new user.  You simply have to override the control’s <createuserwizardstep> template and customize the control layout to have things look how you want. 

 

The sample is using the ASP.NET validation controls to perform client-side validation on the inputs as well within the template (example: making sure passwords match, the age is a valid integer, etc).  One added benefit in ASP.NET 2.0 is that these validation controls now support client-side validation for FireFox and other modern browsers (note: all screenshots were done using FireFox).

There are then three additional properties (their country, gender and age) that I wanted to gather up about the new user as part of the registration process.  Doing this was pretty easy using the new ASP.NET 2.0 Profile system – simply add their definitions within the <profile> tag of the web.config file to register them and store their values in the new profile system:

Listing 1

<profile enabled="true">
    <properties>
        <add name="Country" type="string"/>
        <add name="Gender" type="string"/>
        <add name="Age" type="Int32"/>
    </properties>
</profile>

I then handled the “CreatedUser” event on the CreateUserWizard control within my CreateNewWizard.aspx.cs code-behind file to retrieve the values from the controls within the CreateUserWizard control template and set them in the profile store:

Listing 2

// CreatedUser event is called when a new user is successfully created
public void CreateUserWizard1_CreatedUser(object sender, EventArgs e) {
   // Create an empty Profile for the newly created user
   ProfileCommon p = 
(ProfileCommon) ProfileCommon.Create(CreateUserWizard1.UserName, true);
   // Populate some Profile properties off of the create user wizard
   p.Country = 
((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Country")).SelectedValue;
   p.Gender = 
((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Gender")).SelectedValue;
   p.Age = 
Int32.Parse(((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Age")).Text);
   // Save profile - must be done since we explicitly created it 
   p.Save();
}

Because the user is being created as part of this step, I explicitly choose to create a new Profile object in code (note that I was passing in the CreatedUserWizard1.UserName property as the username – since the user isn’t logged into the system yet).  I then accessed the controls within the template of the <asp:createuserwizard> control, pulled out their values, and stuck them within the newly created profile.  Calling p.save at the end registered this profile with the new username.  (Note: I’ll walk through how we use this profile data later in a page below).


View Entire Article

User Comments

Title: Thanks for the TRICK   
Name: Ankit Singhal
Date: 2009-12-20 3:05:23 PM
Comment:
Thanks a lot for such a wonderful idea.. it worked for me when used CreatedUser and FinishButtonClick Events in place of the Events u suggested. Great Idea. God Bless you :-)
Title: problem!!!!!   
Name: avinash
Date: 2008-04-07 4:01:15 AM
Comment:
this code works fine with me in a public authorization folder! i need this code to make only role "admin" can create accounts so i placed it in a folder with admins only authorized! but the problem is that t creates the account but don't assign any role!! can any body help with this ?
Title: Roles   
Name: Hossam
Date: 2007-07-11 3:47:45 AM
Comment:
this code works fine with me in a public authorization folder! i need this code to make only role "admin" can create accounts so i placed it in a folder with admins only authorized! but the problem is that t creates the account but don't assign any role!! can any body help with this ?
Title: next step   
Name: dupls
Date: 2007-03-21 1:29:34 PM
Comment:
Is there any tutorial out there that will take this to the next level and place those details into another table in the database?

I've followed Scott through a zillion sites he is truly amazing. Very helpful code!
Title: Ruler of the Universe   
Name: Rick
Date: 2007-03-08 7:39:47 PM
Comment:
Not too many comments here, but I think this is a fantastic soution. Thank you for sharing with us, Scott.
Title: Mr   
Name: Pilling
Date: 2006-11-14 11:17:50 AM
Comment:
Anyone have the rolls bit in vb?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-07-22 8:10:24 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search