Thursday, 24 May 2012

Form Authentication in Asp.Net 4.0

First MemberShip & Role Management & Profile Tables should be created. ASP.NET framework provides inbuilt tables, views,stored procedures etc.,

Step 1) Open VS2010 command prompt
  or
located in  C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat


Type  aspnet_regsql.exe(as shown above)

following screen will appear

Press Next button
Select Configure SQL-Server for application services --> Click Next button

Enter Server Name, select Windows Authentication  or enter user name & password.

Leave <default> for database-->  Click Next Button
In this  Sql-Server Name: Sp2010
          Databasename: aspnetdb
Click Next Button --> FINISH 

Step 2)  Open SQL-Server Management Studio
                      aspnetdb database and tables will be displayed.as shown below.

  So far We have added membership,role management & profile management support only(as shown in the above screen).

Step 3)  Now create a Empty Web Site using .net 4.0


in Web.config file 
1) Add connection String
    <connectionStrings>
    <add name="userAuthConn" connectionString="Data Source=sp2010;Initial Catalog=aspnetdb;Integrated Security=True;trusted_connection=yes"/>
  </connectionStrings>

2)  Add rolemanagement,membership & profile sections
    <system.web>
        <compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
      <membership>
        <providers>
          <add name="MembershipProvider" 
               type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
               connectionStringName="userAuthConn" 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="ProfileProvider" 
               connectionStringName="userAuthConn" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
      </profile>
      <roleManager>
        <providers>
          <add name="RoleProvider" connectionStringName="userAuthConn" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
          
        </providers>
      </roleManager>

    </system.web>