Membership Provider Configuration


That's one of the things that makes you lose your temper; Membership provider configuration, in this post I will give you a brief about how to properly configure membership provider in web.config for SQL Server 200x database, and honestly speaking if you try to do it directly, you will go through few trials before you configure it properly, also if you search around you will not find a 1-2-3 guide to configure membership provider model.
Provider Model is one of the best innovations of .NET 2.0, simply Provider Model provides you with the interface, and gives you the choice for implementation, for example if you want to implement membership for your web application, you can use Membership provider that provides all the interface for managing the application users, and for the data you have the option to save it in SQL Server database, or in Active Directory, and the previous 2 providers are coming with .NET 2.0, if you want to save your users' data in other data store, like DB2 or XML, you can just implement the interfaces of Membership, moreover you can build it based on one default provider like SqlMembershipProvider, if you want to start from scratch you can inherit from System.Web.Security.MembershipProvider.
Provider Model is so beautiful if we consider maintenance and flexibility; actually the runtime is reading one configuration value from web.config to know the data type of the provider implementation to use, so you can provide more than one implementation and by setting implementation in web.config the proper implementation will be use, that's so intersting if you want to provide your customers with more than one option; suppose that you're selling your application to different customers, some customers will be using Oracle, while others may be using SQL Server, so the best solution is to build two providers one for SQL Server and the other for Oracle and in your documentation you can provide information about how to configure the application to point to one database; either SQL Server or Oracle.
Note: if you have used DotNetNuke before, you would be familiar with what I am speaking about.

Membership Configuration for non-Express database

While Configuring the Membership model is so simple for express databases, it's not clear for SQL Server 2000,2005 databases, Simply for Express databases once you launch the ASP.NET Configuration web tool, visual studio will create a new express database, aspnetdb, and place it inside App_Data special folder, and it will write required tags to web.config too, and if you click Provider tab and then Select a single provider for all site management data, you should see your default AspNetSqlProvider there, that's for the express database, and surely you should have the SQL Express edition installed, we'll derive the following steps to make our membership module use another database.
1. aspnet_regsql tool :

You can launch this tool by browsing to the .net 2.0 framework and running aspnet_regsql.exe, or you can launch Visual Studio 2005 Comman Prompt, and type aspnet_regsql, this will run ASP.NET SQL Server Setup Wizard, in first screen you will get a brief about the tool, press Next, you will 2 options to select from, first is to append or configure SQL Server Database for application services, or to remove the application services from previously configured database, for now click next as we need to configure our application database, in the next screen you will get to select a database server and then you should choose your database, this will add all the tables, views, and stored procedures needed to setup membership, profiles, role management, personalization, and SQL Web event provider, after you configure all these services successfully, go to your database and check for all recently added database objects.

2. Web.Config Editing

first, you should know a couple of things; that the default connection string name when using the express database is LocalSqlServer, and the default name for the membership provider, when using the express database, is AspNetSqlMembershipProvider, and when you use a different database other than the express one, you should override the default LocalSqlServer setting, so your code should be like this :

<add providername="System.Data.SqlClient" connectionstring="Data Source=CAIROCAFE;Initial Catalog=AdventureWorks;Integrated Security=True" name="MyConnectionString">

When adding the clear tag, this clears the default connection string name, the rest is to configure the membership provider, noting that for the default provider you have AspNetSqlMembershipProvider as the default membership provider name, this merely works with the default express database, so we also need to clear this value as we did with the default connection string, so the membership block will be like this :

<remove name="AspNetSqlMembershipProvider">
<add name="MySqlServerMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionstringname="MyConnectionString" enablepasswordretrieval="false" enablepasswordreset="true" requiresquestionandanswer="true" applicationname="/" requiresuniqueemail="false" passwordformat="Hashed" maxinvalidpasswordattempts="5" passwordattemptwindow="10" passwordstrengthregularexpression="">

The remove tag clears the default membership provider, now if you launch the ASP.NET Configuration web tool, and select Provider tab, then Select a different provider for each feature (advanced), you will find MySqlServerMembershipProvider listed under Membership Provider section.

Note: If you want to use Oracle as a data store for membership, you can develop a custom provider by implementing all interfaces of Membership, or you can start right from the others have ended, I recommend that you have a look at Custom Membership Provider for Oracle 10gR2 and ODP.NET, and also you can check this thread: Custom Oracle Membership Provider.


We reviewed the options to save provider based data, and also we gave a step by step guide to configure Membership Provider with another non-express database, and you can do the same with RoleProvider and ProfileProvider by clearing the default provider names, the default names are AspNetSqlRoleProvider and AspNetSqlProfileProvider respectively.