RSS

Tag Archives: wizard

ASP.NET 4.0: Integrating Membership Roles into the Registration process of the CreateUserWizard

This post assumes you have an ASP.NET project with Membership providers set up. If you’re having trouble with this, you can refer to my previous post.

In my project, I have User Roles and Admin Roles. User Roles are low privilege roles that allow users to interact with the server application. For example if you create an application for a gym management system, you can define “Trainer” and “Trainee” as User Roles. When a trainer or trainee would like to create an account on the portal, he can choose what type of account he wants. Different roles will offer a different webpage with different features. The key here is to ensure that these two roles are not necessarily privileges. They are merely for the system to distinguish between different types of users.

Only users can be added via the CreateUserWizard. Admins, and Roles can be added and modified via the ASP.NET Web Site Configuration Tool. Roles are classified as User Roles or Admin Roles in their “Description” attribute which is a default column in the Roles table (and which I filled up with “User Role” or “Admin Role” directly using SQL Server Management Studio).

When a user registers via the CreateUserWizard, he should be able to choose his role which can only be of type User Role. Therefore I look through the Roles table and filter Roles whose “Description” is “User Role” and populate these in a drop-down list.

The default CreateUserWizard in the new ASP.NET web application project created by Visual Studio 2010 has two steps: Sign Up for Your New Account & Completed.

I want a user to be able to choose his User Role in the Sign Up for Your New Account Step.

1) In design view click on the tag to open Tasks dialog and choose Edit Templates. This shows you the CreateUzerWizard in the edit templates mode. Find the tag again and open the Tasks dialog and choose “Content Template” under Sign Up for Your New Account in the “Display” dropdown list.

2) Click on the point where you want the Roles options to be inserted and hit “Enter” to create a new ‘p’ tag. Add a “Role: ” label whose AssociatedControlID is “RoleList”, to be defined later.

3) Drag and drop an SqlDataSource there. In the tag, click on Configure Data Source. Choose the database (or the connection string to the database) that has already been configured through Aspnet_regsql.

4) Select “Specify columns from a table or view”. I chose vw_aspnet_Roles. I also opened the Where dialog as shown below.

5) Drag and drop a DropDownList at the desired location and call it “RoleList”. Click on its tag and choose Data Source as the one defined in steps 3 and 4. I chose RoleName for both Data Text Field and Data Value Field.

Now we can test the appearance of the form by opening the website and going to the Registration page and seeing the CreateUserWizard. It works for me- I see my DropDownList and it shows me my available User Roles.

6) Finally we need to customize the RegisterUser_CreatedUser method in Register.aspx.cs to ensure that Roles are updated in the database when the CreateUserButton is pressed.

This can be done with 2 simple lines of code. The first line identifies the RoleList DropDownList in the control hierarchy. The second line retrieves the selected role name from the DropDownList and updates the database.

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
            DropDownList RoleList = RegisterUserWizardStep.ContentTemplateContainer.FindControl("RoleList") as DropDownList;
            Roles.AddUserToRole(RegisterUser.UserName, RoleList.SelectedValue);
            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }
 
Leave a comment

Posted by on May 3, 2011 in .NET Framework Development, ASP.NET

 

Tags: , , , , , , , , , , , , , , , , ,