Sukumar Raju's Blog

MCP

Sponsors

Tags

News

SharePoint SharePoint

More reading these days Patterns and practicces


Interesting to work with ASP.NET Membership provider

Suggested Reading C# Book


patterns & practices Application Architecture Guide 2.0


MVP Blog Badge.

Grab this badge here!


Configuring web application to utilise ASP.NET Application Services database

In my article Install ASP.NET Application Services database step by step guide is provided to install ASP.NET Application Services database regard less of SQL Server version. Installing / configuring Application services database allows multiple applications to utilise the database to implement application authentication and authorization.

After installation of services database, it is required to configure ASP.NET application to utilise the ASP.NET Services database(called as Membership database as well).

I come across multiple number of repeating postings at www.asp.net/forums with exceptions when post holders try to utilise ASP.NET application services database to authenticate or authorise end users. Common exceptions noted are ‘sql exceptions’ or ‘Invalid end user credentials when tried to use Login control’ or similar exception.

The reason behind most of these exceptions are not configuring web application to utilise SQL Server installed ASP.NET Application Services database. When responding to those repeating postings on asp.net forums i decided to write an article explaining this process instead of repeating the same info multiple times.

This article explains step by step process of configuring ASP.NET Web application to utilise SQL Server installed ASP.NET Application Services database.

  1. Copying and configuring connection String
  2. Copying and configuring Membership, Roles and Profile sections
  3. Using ASP.NET Web application Configuration tool to choose providers

Hope you find it helpful.

Step 1

Copying and configuring connection String

Note that configuration settings in Web.config file are inherited from machine.config file on your machine. In order to configure web application to utilise Application Services database it is required to copy related sections from machine.config file, which is located at

C:\windows\Microsoft.NET\Framework\v2.0.50727\CONFIG

NB:- Make sure that you are not making any changes to your machine.config file.

To Do:- Copy connection string from machine.config file as shown below.

<connectionStrings>
	<add name="LocalSqlServer" 
	connectionString="data source=.\SQLEXPRESS;
	Integrated Security=SSPI;
	AttachDBFilename=|DataDirectory|aspnetdb.mdf;
	User Instance=true" 
	providerName="System.Data.SqlClient"/>
</connectionStrings>
To Do:- Paste above connection string from machine.config into Web.config and 
change required properties as shown below.
<add name="<Connection String Name>" 
connectionString="Server=<SQL Server NAME>;
Database=<ASP.NET Application services Database NAME>;
User ID=<user ID>;
Password=<password>"
providerName="System.Data.SqlClient" 
/>
Step 2
Copying and configuring Membership, Roles and Profile sections
Note that depending on your application requirements you need either or combination 
or all of these three sections.
  1. Membership
  2. Roles
  3. Profile

To Do:- Copy Membership, Roles and Profile sections from machine.config into Web.config and configure required properties.

<membership>
	<providers>

	<add name="AspNetSqlMembershipProvider" 
        type="System.Web.Security.SqlMembershipProvider,
	System.Web, Version=2.0.0.0, Culture=neutral, 
	PublicKeyToken=b03f5f7f11d50a3a" 
	connectionStringName="LocalSqlServer" 
	enablePasswordRetrieval="false" 
	enablePasswordReset="true" 
	requiresQuestionAndAnswer="true" 
	applicationName="/" 
	requiresUniqueEmail="false" 
	passwordFormat="Hashed" 
	maxInvalidPasswordAttempts="5" 
	minRequiredPasswordLength="7" 
	minRequiredNonalphanumericCharacters="1" 
	passwordAttemptWindow="10" 
	passwordStrengthRegularExpression=""/>
	
	</providers>
</membership>

<profile>
	<providers>
	
	<add name="AspNetSqlProfileProvider" 
	connectionStringName="LocalSqlServer" 
	applicationName="/" 
	type="System.Web.Profile.SqlProfileProvider, 
	System.Web, Version=2.0.0.0, Culture=neutral, 		
        PublicKeyToken=b03f5f7f11d50a3a"/>
	
	</providers>
</profile>

<roleManager>
	<providers>

	<add name="AspNetSqlRoleProvider" 
	connectionStringName="LocalSqlServer" 
	applicationName="/" 
	type="System.Web.Security.SqlRoleProvider, 
	System.Web, Version=2.0.0.0, Culture=neutral,
	PublicKeyToken=b03f5f7f11d50a3a"/>
	
	</providers>
</roleManager>
To Do:- After copying above sections into Web.config, make sure you modify 
minimum required attributes such as name, connectionStringName and
ApplicationName
  • Note that depending on your application requirements you may modify other attributes mostly in Membership section.

After modifying minimum attributes in Membership, Roles and Profile sections, these sections looks similar as shown below.

<membership defaultProvider="AspNetMembershipProvider">
	<providers>
	<add connectionStringName="<AspNetServices 
	Connectionstring Name from connectionstrings
	section" 
	enablePasswordRetrieval="false" 
	enablePasswordReset="true" 
	requiresQuestionAndAnswer="true" 
	applicationName="<WEB APP NAME>" 
	requiresUniqueEmail="false" 
	passwordFormat="Clear" 
	maxInvalidPasswordAttempts="5" 
	minRequiredPasswordLength="7" 
	minRequiredNonalphanumericCharacters="0" 
	passwordAttemptWindow="10" 
	passwordStrengthRegularExpression="" 
	name="AspNetMembershipProvider"
        type="System.Web.Security.SqlMembershipProvider, 
	System.Web, Version=2.0.0.0, Culture=neutral,
        PublicKeyToken=b03f5f7f11d50a3a"/>
	</providers>
</membership>

<roleManager enabled="true" defaultProvider="AspNetRoleProvider">
	<providers>
	<add connectionStringName="<AspNetServices 
	Connectionstring Name from connectionstrings
	section" 
	applicationName="<ASP.NET application NAME>" 
	name="AspNetRoleProvider" 
	type="System.Web.Security.SqlRoleProvider, 
	System.Web, Version=2.0.0.0, Culture=neutral,
        PublicKeyToken=b03f5f7f11d50a3a"/>
	</providers>
</roleManager>

<profile>
	<providers>
	
	<add name="<ProfileProvider Name>" 
	connectionStringName="<From above connectionStirngs
	section that is pointing to Servies database>" 
	applicationName="/<Your ASP.NET app Name>" 
	type="System.Web.Profile.SqlProfileProvider, 
	System.Web, Version=2.0.0.0, Culture=neutral,
        PublicKeyToken=b03f5f7f11d50a3a"/>
	
	</providers>
</profile>
NB:- Make sure that application Name property is set at all the time as
suggested by Scott Guthrie here

Step 3

Using ASP.NET Web application Configuration tool to choose providers

After adding and modifying required sections as explained above, save your Web.config and configure your web application to utilise ASP.NET Application Services Database as explained below.

To Do:- Start ASP.NET Configuration tool from Website menu (shown below). Note that ASP.NET configuration tool can be initiated from Solution Explorer menu as well. 

ASP.NET Configuration Tool

Selecting ASP.NET Configuration opens Web Site Administration Tool in browser.

To Do:-Select Provider Configuration hyperlink as shown below.

Config_Tool

Selecting Provider Configuration navigates to Provider page where you can choose either Single provider or different provider for each feature as shown below.

Single_Provider

  • To Do:- You can choose either of the above options available. For this tutorial choosing ‘Select a different provider each feature’ option. By doing so each providercan choose different data sources.

Selecting Select a different provider for each feature (advanced) hyperlink navigates to next page where you can select a provider for each feature as shown below.

Provider_Selection

On this screen you can see provider name(from provider section), choose a provider

for each feature i.e., Membership, Roles and Profile and select Ok.

Thats it. You are ready to fly! Configuration is done.

  • Testing:- In order to make sure your ASP.NET application services database is configured to be used by web application, you can create a new user by selecting Security tab with in Web Site Administration tool, then Create User hyperlink. After creating the user make sure that you see the same information in ASP.NET Application Services database aspnet_Users

References

Comments

Hyousef said:

Nice article, but how to setup Profiles in ASP.net , if you can detail that step by step, that would be great.

Thanks

# April 23, 2010 11:36 AM

progammer said:

thank you brother it is important subject i need it

# September 15, 2010 9:15 AM

Abubakar said:

Good! really love that.

# November 2, 2010 10:42 AM

generic viag`З said:

graet site. Keep doing

# March 22, 2011 1:53 PM

sildenafil said:

Do you have any more info on this?

# March 26, 2011 8:56 AM

sukumarraju said:

Hi sildenafil,

Let me know the more info you are looking for, can provide further.

Thanks

# March 26, 2011 3:36 PM

weblogs.asp.net said:

Configuring web application to utilise asp net application services database.. Neat :)

# April 4, 2011 7:05 AM

weblogs.asp.net said:

Configuring web application to utilise asp net application services database.. Very nice :)

# April 30, 2011 2:43 AM

Pepe said:

Well it is good, but missing a few things...

For instance in Step 1, when editing your web.config file to connect to the application database; what user should that be?  An existing one? Which one?  A new one?  What rights should it have? etc?

# June 7, 2011 3:40 PM

weblogs.asp.net said:

Configuring web application to utilise asp net application services database.. May I repost it? :)

# June 13, 2011 4:27 AM

sukumarraju said:

Yes, you can repost as it helps developers!

# July 24, 2011 4:36 PM

Gaspard said:

Thank you for this article!

# November 21, 2011 9:11 PM

Pithecanthropus said:

Sukumar, thanks so much providing this! It does help clarify things, especially to users who don't want to get overly bogged down in authorization/authentication issues.  There are times when, while realizing that we will need these capabilities at launch time, we would prefer to defer such specific details to a later time.

However: in following the instructions I am still unable to get my web app working, and suspect I may have corrupted something which the steps above won't correct (for example, my machine.config file).  Apart from the fact that the configuration tool doesn't work, the web app itself will not run and throws the error that the role provider has already been defined.

At the very least, I'm thinking I need to reinstall the version of ASP.NET which I'm trying to use. Or do I need to go all the way and reinstall VS2010?

# May 15, 2012 5:35 PM

Anna said:

Hi sukumarraju,

I want to develop an Administrator website which can assign different users different access rights for different website. Is there any suggestion for this?

Is there any way I can select,modify & add Application Name, Role name to ASP.net_Applications ,ASP.net_Roles ...table?

# September 21, 2012 11:36 PM

sukumarraju said:

Hi Anna,

Refer www.4guysfromrolla.com/.../052307-1.aspx

You can download and customize this to meet your requirements.

# September 30, 2012 4:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)