ASP.Net Login Controls: Custom Membership Provider for SQL Server

Monday, December 01, 2008

ASP.Net provides excellent controls for managing user logins in your web application. But some applications require custom database fields and functionality for their user tables. The ASP.NET login controls can be used in these applications by writing a custom membership provider.

This msdn tutorial guides you on the implementation steps required for implementing a membership provider. An ODBC based sample implementation can be found here. This shows the sample database schema for implementing a membership provider on MS-Access. The corresponding C#/VB.NET code can be found on: How to: Sample Membership Provider Implementation.

I have modified this custom Membership Provider's ODBC implementation for SQL Server. The file can be downloaded from here. This file also contains the custom Create Table SQL for the user table. This membership provider should be saved as .cs file and be placed in the 'App_Code' folder. The web.config file should be configured to use this membership provider. This can be done by adding the following between the <system.web> tags:

    <membership defaultProvider="DbMembership">
<providers>
<add name="DbMembership"
type="DbMembership.DbMembershipProvider"
connectionStringName="SqlConnectionString"
enablePasswordRetrieval="true"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
writeExceptionsToEventLog="true"
applicationName="AFIRS"/>
</providers>
</membership>
After adding this all the ASP.NET login controls will be used according to the functionality specified in the custom membership provider DbMembership.