This tutorial shows you how to use the
CreateUserWizard control with two SQL Membership Providers in an
ASP.NET Web Application, and how to programmatically switch between the
two. C#.
Using Two ASP.NET Membership Providers to CreateUser C#
This tutorial shows you how to use the CreateUserWizard control with
two SQL Membership Providers in an ASP.NET Web Application, and how to
programmatically switch between the two. C#.
In this tutorial, we will demonstrate how to use two
different ASP.NET Membership Providers to utilize two separate SQL
Server Databases for creating users. By Default, ASP.NET Membership
provides you with powerful tools to implement a membership system into
your web application.
Even with all the power and functionality we get with the Membership
class, it is surprisingly easy to work with and implement, as well as
customize. In this tutorial, you will learn how to utilize two SQL
Membership Providers to register users for two separate databases.
To start, we will create a new Web Site in Visual Studio, and then
click Website > ASP.NET Configuration. Next, select the Security tab
and click either the 'Use the security Setup Wizard to configure
security step by step' or the 'Select authentication type' link.
Whichever method you choose, you will want to select the Access method
'From the internet'. For this example, we will not enable Roles or
create users just yet.
Once we have set the Access method, we can close the browser for the
Configuration. You should now notice that Visual Studio has created a
database for our Membership. Right-click the App_Data folder in
Solution Explorer, then choose Refresh - you will notice a SQL Server
database named ASPNETDB.MDF
Before we do anything else, let's Copy and Paste this database in
Solution Explorer. Rename the copied database. Because we used ASP.NET
Configuration to set-up the Membership class, by default the database
being used is the ASPNETDB.MDF
Now to add our Secondary SQL Provider, we will need to edit the Web.config. First, add the connection strings for each database:
|
<connectionStrings>
<add
name="connString1" connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated
Security=True;User Instance=True"/>
<add name="connString2"
connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB2.mdf;Integrated
Security=True;User Instance=True" />
</connectionStrings>
|
Inside the <system.web> tags, add the following:
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx">
<credentials passwordFormat="Clear" />
</forms>
</authentication>
<membership defaultProvider="Provider1">
<providers>
<add connectionStringName="connString1" applicationName="Application1"
minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0"
name="Provider1" type="System.Web.Security.SqlMembershipProvider" />
<add connectionStringName="connString2" applicationName="Application2"
minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0"
name="Provider2" type="System.Web.Security.SqlMembershipProvider" />
</providers>
</membership>
|
Notice that our default provider is the one that ASP.NET created automatically for us. Then we basically just duplicate it.
The next step for us is to create a Register User page. The easiest way
to do this is to use the CreateUserWizard ASP.NET control, which can be
used with Custom Providers, as they inherit from the default class.
Create a Register.aspx and drag on from the Toolbox a CreateUserWizard
control. You should have something like this:
<form id="form1" runat="server">
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</form>
|
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers
and add-ons were setup swiftly, in less than 24 hours. We were able to
confirm our order over the phone. They respond to our inquiries within
an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
If we run this page right now, the Wizard will use the default
Membership Provider to create a new user. So let's add an option to
choose which provider to use; let's add a RadioButtonList:
|
<asp:RadioButtonList ID="rad_MembershipProvider" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="rad_MembershipProvider_SelectedIndexChanged">
<asp:ListItem Text="Provider1" Value="provider1" Selected="True" />
<asp:ListItem Text="Provider2" Value="provider2" />
</asp:RadioButtonList>
<br />
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
|
Notice that we have also added an OnSelectedIndexChanged event
handler to the RadioButtonList, as well as set AutoPostBack to true.
This is because we want to change the Provider when the option is
selected. To do this, we simply add the following in the code-behind:
protected void rad_MembershipProvider_SelectedIndexChanged(object sender, EventArgs e)
{
if (rad_MembershipProvider.SelectedValue == "provider1")
{
CreateUserWizard1.MembershipProvider = "Provider1";
}
else
{
CreateUserWizard1.MembershipProvider = "Provider2";
}
}
|
Now when this page is run, we can programmatically set the
Membership Provider at runtime, allowing us to add users to two
separate ASP.NET Membership tables. To make the process a little
cleaner, we could add an AJAX UpdatePanel to mask the PostBack on
selecting a Radio option:
<form id="form1" runat="server">
<asp:ScriptManager ID="SM1" runat="server" />
<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="rad_MembershipProvider" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="rad_MembershipProvider_SelectedIndexChanged">
<asp:ListItem Text="Provider1" Value="provider1" Selected="True" />
<asp:ListItem Text="Provider2" Value="provider2" />
</asp:RadioButtonList>
<br />
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</ContentTemplate>
</asp:UpdatePanel>
</form>
|
The entire code-behind will look something like this:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rad_MembershipProvider_SelectedIndexChanged(object sender, EventArgs e)
{
if (rad_MembershipProvider.SelectedValue == "provider1")
{
CreateUserWizard1.MembershipProvider = "Provider1";
}
else
{
CreateUserWizard1.MembershipProvider = "Provider2";
}
}
}
|
|