Introduction

The next stage in adding functionality to the user administration section of our ASP.NET Administration template is to allow ourselves to create new users and assign them the roles that we created in the previous article. We will also of course allow End Users (EUs) to register themselves and automatically assign them the minimum authenticated role we have.

Building the Sample

This code has been written on Windows Server 2008 RC2, and developed using Microsoft Visual Studio 2010 It should work perfectly well on any computer running Windows 7 and higher (possibly Vista) and you should be able to use it within your Visual Studio 2012 environment.

You will need to add your own connection strings to the solution; found in the Web Config file.

The application will NOT build. This is deliberate - there is a small error concerning the creation of the user. It is your task in this example to work out what the error is and where the information is that will let you fix the error. You will also want to create your messages for successfully creating the user as well as failing to create the user.

Description

I am not going to go into great depths about user management, authorisation and authentication - there are complete books on the subject. At the end of this article there are links to some excellent resources that you should read if you wish to turn this tutorial work into a production ready solution.

Ok, so we know now how to create roles and you will have added the logic for updating and deleting roles. Now we need to create the Users who we will assign roles to. In this tutorial we are of course only interested in the back-end administration aspects of user management and not the user's self-registration process - I will leave that to you; although I will show you how to assign a role to their registration proceedure.

The natural reason for an administrator to want to add user's manually is so that they can assign special priviledges to an individual or for creating test users for themselves with each individual role assigned.

Remember to rate these articles please. Without the ratings I can not tell if I am giving too much or not enough information!

Procedure

  1. You should already have your connection string set-up from previous articles. If you use the download available from this article you will need to re-set the connection string again in web.config
  2. In your solution explorer add a new item, web form using admin.master file. Save it as ManageUsers.aspx
  3. Get rid of any unnecessary content holders in the new file - so that the admin.master content is shown

The basic setup following the proceedure we designed in the Roles tutorial produces this:

You will notice the ApplicationId's are different. This is from previous work I have done. You need to be aware that you need to filter your users by ApplicationId before displaying them, to End Users, although it is ok for the Super Administrator - if and only if - the Super Administrator is the same for all applications using this database.

So the first thing we need to do is filter the users according to our current application id, which means we need to know what our application id is. Our aspnetdb contains an Application table where we can look up the ApplicationId using the application name which you set in your web config.... correct? If you did not do that, then do it now; If this application is to work on a web farm it needs to have a fixed Application Name and not an ASP.NET auto-generated one.

 

C#
Edit|Remove
// Get the applicationID from Applications table. 
        private Guid getApplicationID() 
        { 
            string appName = "cmsCCSLABS"; 
            Guid aID = new Guid(); 
 
            try 
            { 
                // Connect to Database 
                SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString); 
                SqlCommand command = new SqlCommand("SELECT (ApplicationId) FROM aspnet_Applications WHERE ApplicationName='" + appName + "';", conn); 
                conn.Open(); 
                aID = (Guid)command.ExecuteScalar(); 
                conn.Close(); 
            } 
            catch (Exception ex) 
            { 
                string exx = ex.Message; 
 
            } 
 
            return aID; 
        }
 The above method gets the ApplicationId from aspnet_Applications table. You will have to check to see if your table is called the same. I have hard coded my appName = although we should have this in our settings, or extract it from our web.config. This gives us the ApplicationId from our ApplicationName - now we can limit the usernames to our running application.
So, with what we have done so far - now, we still have to add the functionality to Add, Delete and Update Members. Then, we will need to add members to roles. So we have quite a lot to do yet. However, in this tutorial we are simply going to produce the functionality to add, delete and update. In the next sub-tutorial on User Administration we will combine what we have done so that the membership system and the roles system work in unison.
Adding, Deleting, and Updating, are the basics of all user administration; although additional considerations are often required such as suspended accounts, or frozen accounts because of investigations due to litigation. These additional considerations could be placed in the membership database however, it is probably better to create a new table which holds additional information such as profiles and statuses, messages and notes etc.
Once we have added the labels, textboxes and link buttons for adding, updating and deleting we simply double click each button to create the event and move to the code behind method so we can implement the required functionality.

Roles were created using Roles.Add, Users, are created using Membership.CreateUser, and requires a minimum of Username and Password. That is all I have implemented for you; I am sure you can work the rest out for yourself. I will also leave you to work out how to update and delete the users.

 

More Information

Team View Service, Azure Services and Azure SQL

  1. https://tfspreview.com/en-us/learn/start/connect-to-vs/
  2. http://blogs.msdn.com/b/bharry/archive/2012/06/07/announcing-continuous-deployment-to-azure-with-team-foundation-service.aspx

Authentication and Authorisation

  1. http://www.4guysfromrolla.com/articles/121405-1.aspx
  2. http://weblogs.asp.net/scottgu/archive/2006/04/22/Always-set-the-_2200_applicationName_2200_-property-when-configuring-ASP.NET-2.0-Membership-and-other-Providers.aspx
  3. http://msdn.microsoft.com/en-us/library/t32yf0a9.aspx

 

However, I will not add these tutorials if no one is interested in this, so if you are interested then rate this article please.