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

<asp:createuserwizard> wizard step 2: picking roles

After the user fills out the first step of registration information and clicks the next button, the user is created, the CreatedUser event fires, and we fill in the appropriate information about the user into the profile store. 

 

The <asp:createuserwizard> control then displays the second step template we’ve defined.  This template lists all of the roles currently created in the ASP.NET Role Management system, and allows the user to select which roles they belong to:

Figure 3

Note that you’d typically never assign roles to an end-user this way (instead you’d add your own logic to somehow calculate which role they belong in), but I thought this scenario was cool nonetheless which is why it works like the sample above.

I think it is cool because of the way we populate and access these roles in the template of the <asp:createuserwizard>.  Basically, we have a template definition to the .aspx like this:

Listing 3

<asp:WizardStep runat="server" AllowReturn="False"
  OnActivate="AssignUserToRoles_Activate"
  OnDeactivate="AssignUserToRoles_Deactivate">
    <table>
        <tr>
           <td>
              Select one or more roles for the user:
           </td>
        </tr>
        <tr>
            <td>
               <asp:ListBox ID="AvailableRoles" runat="server" 
                            SelectionMode="Multiple" >
               </asp:ListBox>
            </td>
        </tr>
    </table>
</asp:WizardStep>

It simply contains a <asp:listbox> control named “AvailableRoles”.  When this wizard step is loaded (after the user hits the next button on the first step in the wizard), it will fire the “OnActivate” event.  We can use this to databind the list of all roles in the system to the above listbox.  When the user hits next again, the wizard will fire the “OnDeactivate” event.  We can then use this to determine which roles were selected in the above listbox, and use them to update the role-manager system.

The code to-do both of these actions looks like this:

Listing 4

// Activate event fires when user hits "next" in the CreateUserWizard
public void AssignUserToRoles_Activate(object sender, EventArgs e) {
    // Databind list of roles in the role manager system to listbox
    AvailableRoles.DataSource = Roles.GetAllRoles(); ;
    AvailableRoles.DataBind();
}
// Deactivate event fires when user hits "next" in the CreateUserWizard 
public void AssignUserToRoles_Deactivate(object sender, EventArgs e) {
    // Add user to all selected roles from the roles listbox
    for (int i = 0; i < AvailableRoles.Items.Count; i++) {
        if (AvailableRoles.Items[i].Selected == true) 
          Roles.AddUserToRole(CreateUserWizard1.UserName, AvailableRoles.Items[i].Value);
    }
}

That is all of the code for the CreateNewWizard.aspx.cs file – 17 lines total if you omit comments and whitespace (if my count is right). 


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-2024 ASPAlliance.com  |  Page Processed at 2024-10-07 8:14:27 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search