Friday, December 4, 2009

Using Membership Provider with security controls in ASP.NET

Membership Provider is a great feature of ASP.NET which makes user authentication jobs dramatically easier for coders. There are two different membership providers: the ActiveDirectoryMembershipProvider and the SqlMembershipProvider.
The ActiveDirectoryMembership provider uses Microsoft® Active Directory® directory service to maintain user information, while the SqlMembershipProvider stores user details in a SQL Server database.
This article is aimed to provide the minimum required steps to use Membership Provider with security controls.

First create a database which will store your user credentials. Suppose the name of the database is Credentials.

First start Visual Studio 2008 Command Prompt,assuming you are using VS2008 you will find it in All Programs>Microsoft Visual Studio 2008>Visual Studio Tools>Visual Studio 2008 Command Prompt.

Now execute the command aspnet_regsql Now you will be given a ASP.NET SQL Server Setup Wizard. You can use this tool to create/configure databases which stores information of ASP.NET application services.

If you have successfully followed the steps then check the database you provided here and you will find some new tables created in your database. So your database is ready.

Now create a web site using VS2008. We have some editing to do in the web.config file. You have to provide connection sttring,changed authentication mode to "Form" from "Windows" and then in system.web section add membership provider info.

<>
< name="ConnectionString" connectionstring="server=BDDH01-W0065\SQL2K5;database=Library;integrated security=SSPI;Trusted_Connection=Yes;">
< /connectionStrings >

and ---

< mode="Form">

and finally in system.web section

< defaultprovider="CustomMembershipProvider">
<>
< add connectionStringName="ConnectionString" name="CustomMembershipProvider"
type="System.Web.Security.SqlMembershipProvider" />
< /providers>
< /membership >



That's all. Now to check if everything is working fine go to Website menu in Visual Studio and select ASP.NET Configuration . A webpage will appear and you can configure your system here Go to Security tab,there will be a option for creating new user. Create a new user.

Now add the Login control in your webpage from Tollbox.Run the website and try to log in using the user you already have created. You can use all the other controls like CreateUserWizard etc. in the same way. Easy, huh :) :) :)

So you have managed to create the whole authentication process without any coding. This is just the beginning,there are a lot of other features available. If possible I will come with some deeper facts.

No comments: