Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Database - Using Two ASP.NET Membership Providers to CreateUser VB
Using Two ASP.NET Membership Providers to CreateUser VB

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Database Tutorial

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. VB.NET.

Untitled Document

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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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>

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">
<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>

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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 Sub rad_MembershipProvider_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rad_MembershipProvider.SelectedIndexChanged
If rad_MembershipProvider.SelectedValue = "provider1" Then
CreateUserWizard1.MembershipProvider = "Provider1"
Else
CreateUserWizard1.MembershipProvider = "Provider2"
End If
End Sub

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">
<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>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

The entire code-behind will look something like this:

Partial Class Register
Inherits System.Web.UI.Page

Protected Sub rad_MembershipProvider_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rad_MembershipProvider.SelectedIndexChanged
If rad_MembershipProvider.SelectedValue = "provider1" Then
CreateUserWizard1.MembershipProvider = "Provider1"
Else
CreateUserWizard1.MembershipProvider = "Provider2"
End If
End Sub
End Class

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks