...

Adding sql membership in Asp.Net mvc project

How to add sql membership in Asp.Net mvc project

Steps to follow

  1. Create membership db
  2. Add configuration in web.config
  3. Add membership users & roles

 

1. Create membership db


We are going to create membership tables in our database using command line tool.
First of all open command prompt.
Navigate to directory %WINDOWS%\Microsoft.NET\Framework\v4.0.30319\ in your local file system.
Now lets run command -
aspnet_regsql.exe -S <server> -U <login id> -P <password> -d <database> -A all

Add database credentials like: database server, username and password & database name.
aspnet_regsql.exe -S .\SqlExpress -U sa -P ****** -d testDB -A all
The -A all installs all membership features to install specific like membership and roles use -A mr for most common cases.
command parameters include -
m - membership
p - profile
r - roles
all - include all above features

This will install db schema for membership & roles.

 

aspnet_regsql.exe -S .\SqlExpress -U sa -P ****** -d testDB -A mr.


 

2. Add configuration in web.config


Necessary part is to add configuration settings in web.config.
So lets open web.config file and add defaultconnection in connection string.

<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=ServerName;Initial Catalog=dbName;Integrated Security=False;User ID=sa;Password=*****" /></connectionStrings>

Now add profile, membership & roles.

<profile defaultProvider="SqlProfileProvider" enabled="true">
 <providers>
 <clear/>
 <add name="SqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DefaultConnection" applicationName="App v1.0" />
 </providers>
 </profile>
 <membership defaultProvider="SqlMembershipProvider">
 <providers>
 <clear/>
 <add name="SqlMembershipProvider" connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
 enablePasswordReset="true" requiresQuestionAndAnswer="false"
 requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
 minRequiredNonalphanumericCharacters="0"
 applicationName="App v1.0" type="System.Web.Security.SqlMembershipProvider" />
 </providers>
 </membership>
 <roleManager enabled="true" defaultProvider="SqlRoleProvider">
 <providers>
 <clear/>
 <add connectionStringName="DefaultConnection" applicationName="App v1.0"
 name="SqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
 </providers>
 </roleManager>

 

 3. Add membership users & roles


After we have added asp.net membership configuration select project in Visual studio solution explorer.
We can go to project menu and select Asp.Net Configuration – This will open Web Administration tool in Web browser.
Which will give you a panel to create user roles and login.

 

web-administraion.png, undefined, SQL,

Back to top