How to configure and Implement ASP.NET Membership in your very own ASP.NET Application
If you are an experienced web-programmer, you must be repeating yourself a lot of times in programming a system or service which accepts a username and password and authenticates a user. If you are a fresher or just starting with Web-programming or ASP.NET then it's good that you don't have to take the trouble of making a system to accept username and password to authenticate a user which experienced programmers have been doing for ages now.
Authentication is something which is required on most of the websites or web-applications now a days. Moreover time to time you will find situations where you need to protect some content or sections of your website from unregistered or anonymous users. And in those situations you will have to enable your users to register with you and have there credentials with them to be able to view protected contents of your website. You might also be in a situation sometimes where you need to track some users or check there activities so you can customize your websites according to there personal experience. And in this situation you would prefer to assign a username and password to all of yours users where informations about there interest, there activities can be stored so you can enable to view what they actually like. Similar to these situations you will find a lot of other times when the only solution is to provide a username and password to your users with which they can log in to your website.
Now comes ASP.NET Membership. This is something that does more than just providing a username and password to your users and authenticating them. As the name "Membership" clarifies it is actually a membership system which enables YOU to have your website members in form of users, each of them with unique username and a password.
ASP.NET Membership works with Microsoft SQL Server 2005 or above. It's very easy to enable it or install it in your web application. To start with at first open up command prompt. Then navigate to the the windows drive which is C:\ drive most of the times. Form there navigate to the Windows directory. Then go to Microsoft.NET folder and then to the Framework folder inside it. Now here you might find a few folders starting with v. some might be v1..... and some might start with v2.... .All these folders represent different versions of .NET installed in your computer. Anyways we need to navigate to any folder that starts with v2. which represents the .NET version 2.0 or further. When you are inside this directory you will need to run "aspnet_regsql.exe ". To help you follow these instructions easily I've left a screenshot below of how to navigate to the desired folder and run the aspnet_regsql file.
After you followed the instructions above and ran the aspnet_regsql file you will see another screen came up which looks similar to the image below.
As described in this screen, it is used to configure or set up an Sql Server Database to work with Asp.net Applications. And right now we are going to use it for ASP.NET Membership. To continue doing that we'll click "Next". And we will get another screen like one below.
In this page we need to leave the first option selected that says Configure Sql Server for application services because we are right now configuring the Sql server 2005 for our ASP.NET Membership. (In case we needed to remove that service from any existing database we would have selected the second option.) Now simply click Next again and we'll get another window which will be the one below :
This page requires credentials for Sql Server and name of our database on which we need to set up our ASP.NET Membership. The server name will be the name of your server and it is "localhost" most of the times. (It could also be a different name incase you have provided a different name during installing your SQL Server.) Then Specify the authentication type. Most of the times it will work with Windows Authentication however sometimes it will only work when we specifically provide the SQL Server Username and Password for that. (There are times when it can work in any of the ways you like.)
Now you need to specify a Database name in the Database field. If you click on the dropdown box then you should get a list of existing databases present in you Sql Server. (It might not show you the list and could give an error if the credentials provided above are not correct.) If you already have a database created for your application on which you want to set up ASP.NET Membership then just select the name of your database from the dropdown list. Else you can type a database name and this Wizard will create that database for you and configure it to use ASP.NET Membership. If you are typing a name of a new database then you should be aware that you can use that database to store more tables into it but you should never modify any of the existing tables which are created by ASP.NET Membership. All the tables required by ASP.NET Membership begins with "dbo.aspnet_", so make sure you never modify those tables manually.
Now Click Next again and you will get another confirmation page looking somewhat like this:
Simply click next to confirm. If you have any doubts regarding what you have entered in previous screens you can go back by clicking "Previous" button, else click Next.
After you click Next you will just to hold back for a few seconds (specially if you have a slow computer) and after that you will get an Acknowledgement screen looking like one below:
After you got this screen simply click finish and you are done setting up Sql Server for you ASP.NET Membership. Note that you won't get this last page to click finish if there's any error in connecting to server or if you have not provided valid username and password or if there's any other problem. You will get an error page with error details incase of an error.
Now to verify and check the database created by logging into your Sql Server using SQL Server Management Studio. In the list of database you will see the name of the database you have typed previously. This will contain a list of tables (as I said above) with names starting with "dbo.aspnet_". You should never edit these tables manually.
Now that SQL Server is configured we need to configure your Asp.net Application to use this Membership.
As It has been so far, configuring ASP.NET Application is also a series of simple steps. Now to do this we need to go to the root directory of your ASP.NET Application and find the Web.Config file inside it. In case you don't find Web.Config file present in the root directory then you need to create one. Using Visual Studio 2005 you can create it in the following way as shown in the image.
At first go to the solutions explorer of the visual studio and right click your project name and Click on Add new Item
After you do that you will get a window like this in front of you. Select Web Configuration File from this list and click the Add button. This will get you a web.config file added to the root directory in your application.
The newly added web.config file might look somewhat like this.
We are almost done. We now just need to add a set of few tags to be added to the web.config file after which our ASP.NET Membership will be ready to work for our application.
Now under the configuration tag we need to add connection string which will be used to connect to our ASP.NET membership database.
This is the tag we need to add. This contains connection string. We just add a new connection string with the name we want to give in name attribute and specifying it's connection String (which is similar to connection string used in ADO.NET). We specify the name of our server in "Data Source" and username, password and database name in "UserID, Password and Initial Catalog respectively.
Then comes adding a memebership provider. We add it inside the System.Web tag.
We start by adding the tag of Membership and specify an attribute of default provider and specifying the name of our membership provider that we are just going to add now. Then add a provider tag and add a membership provider inside by specifying a name for it. Then specify the connection string that we created above and add type as System.Web.Security.SqlMembershipPfovider because this is actual membership provider class in .NET . Now you can specify other attributes as required. Like you set requiresQuestionAndAnswer as true or false where true would mean every user will have to have a security question specified with an answer to it so that they can recover there password if lost. requiresUniqueEmail specifies if every user have to have a unique email address or not. maxInvalidPasswordAttempts specifies the number of consecutive times which user can try to login with invalid password after which his account will be locked out. minRequiredNonalphanumericCharacters specifies how many nonalphanumeric characters (like the sign of underscore _ or other special characters like @, #, $... and so on.) are required in the password.
After specifying all these values close providers tag and close membership tag and you have your membership provider added with the name of the provider you gave to it above.
Now you can add a login page to your web application where user can enter there username and password to login. You can achieve it verysimply by creating a page and adding .NET Login Control from the Login Controls list in the toolbox. Optionally you can specify the name of your membership provider to use in the Login Controls attribute of "MembershipProvider".
Congratulations ! You are ready to go. Your membership provider is ready to work and you have also learnt now how to set up the membership provider. Now to use it manuall there are some static methods of Membership class itself which you can use create, block or update users. Like you can use Membership.Create() method to create a membership user by specifying username and password in it's parameter. Refer to Microsoft documentation of Membership class for more details of all the methods you can use.