Uncategorized

ASP.NET Membership Provider with custom schema

PROBLEM DESCRIPTION
===================
Asp.net Membership control issue: when I create users and roles by wizard the default database is sql server express and the file is aspnet.mdf, but in my case I have my own database which store the users detail. I want to use the membership control functionality but don’t want to deal with 2 databases; also, I want to add my table to the default database thus changing the schema.
I expect the Asp.net Membership provider to support that change and want to know what should I change in the application code

SUMMARY of TROUBLESHOOTING
==========================
“How to: Use membership in ASP.NET 2.0” at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/paght000022.asp, in particular the section “Using the SQLMemberShipProvider”; basically you need to configure Forms authentication, install the membership database and configure the SqlMembershipProvider.

Step 2. Install the Membership Database
Before you can use the SqlMembershipProvider, you must install the SQL Server membership database.
To install the membership database, log on to your server with an account that has authority to administrate SQL Server (such as the Administrator account). Open the Visual Studio 2005 command prompt, and run the following command:

aspnet_regsql.exe -E -S localhost -A m

Where:
-E indicates authenticate using the Windows credentials of the currently logged on user
-S (server) indicates the name of the server where the database will be installed or is already installed
-A m indicates add membership support. This creates the tables and stored procedures required by the membership provider

Note:
The Aspnet_regsql tool is also used to install database elements for other ASP.NET 2.0 features, such as Role Management, Profile, Web Parts Personalization, and Web Events. Other command-line arguments perform database operations for these other features. You can use Aspnet_regsql without any command line arguments by using a wizard that allows you to specify connection information for your SQL Server and install or remove the database elements for all of the supported features.

Step 3. Configure the SqlMembershipProvider
The Machine.config file contains a default SqlMembershipProvider instance named AspNetSqlMembershipProvider that connects to the SQL Server Express instance on the local computer. You can use this instance of the provider if you are running SQL Server locally. Alternatively, you can specify provider details in your application’s Web.config file, as shown here in the following example.

<connectionStrings>    <add name="MySqlConnection" connectionString="Data Source=MySqlServer;          Initial Catalog=aspnetdb;Integrated Security=SSPI;" />
</connectionStrings>

<system.web>

...

<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
    <providers>
        <clear />
        <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider"             connectionStringName="MySqlConnection"
            applicationName="MyApplication" enablePasswordRetrieval="false"             enablePasswordReset="true" requiresQuestionAndAnswer="true"
            requiresUniqueEmail="true" passwordFormat="Hashed" />
    </providers>
</membership>

Make sure to set the defaultProvider attribute value to point to your provider definition. The default value points to AspNetSqlProvider, which uses the local SqlExpress instance.

Cheers
Carlo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.