How to Setup Membership and Role Provider in C# Web.config

Often I would create a new project and need to tie up the application in aspnet database. And yet I often forgot how to setup the connection string. So I'm logging them here:

Find the end of the system.web tag: </system.web>
<membership defaultProvider="SQLMembershipProvider">
      <providers>
        <add connectionStringName="aspnetdb" applicationName="applicationName" enablePasswordRetrieval="True" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" minRequiredPasswordLength="1" minRequiredNonalphanumericCharacters="0" passwordFormat="Clear" name="SQLMembershipProvider" type="System.Web.Security.SqlMembershipProvider"/>
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="SqlRoleManager">
      <providers>
        <add connectionStringName="aspnetdb" applicationName="applicationName" name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider"/>
      </providers>
    </roleManager>
Also you need to define the connection string in <connectionStrings>.

<add name="aspnetdb" connectionString="Data Source=Server;Initial Catalog=Database;Persist Security Info=True;User ID=usr;Password=pwd;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>

Note: applicationName, is the entry in the aspnet_Applications table.

I guess you can figure out what the other parameter means.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.