Using two ASP.NET membership providers

Recently I wanted to give someone access to one of my teaching web sites. They were not a student or faculty member so they didn’t have an account in our active directory. I am not a system administrator so I wasn’t able to add them to the active directory either. The solution was to add a second membership provider to my ASP.NET site. The first membership provider is based on active directory. The new, 2nd provider is a SQL provider. I found a great blog post that had the relevant code for checking the username and password against the second provider: http://www.stevideter.com/2008/03/20/using-two-membership-providers-for-aspnet-logins/.

The code behind for the login page was in C# so I thought I would provide the bare bones Visual Basic equivalent:

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

Dim foundUser As Boolean = False

‘this will call the default MembershipProvider
If (Membership.Provider.ValidateUser(Login1.UserName, Login1.Password)) Then
foundUser = True
‘ otherwise, explicitly call secondary provider
ElseIf Membership.Providers(“AspNetSqlMembershipProvider”).ValidateUser(Login1.UserName, Login1.Password) Then
foundUser = True
End If

If (foundUser) Then
e.Authenticated = foundUser
End If

End Sub

The membership section of web.config looks like this:
<membership defaultProvider=”AspNetActiveDirectoryMembershipProvider”>
<providers>
<add name=”AspNetActiveDirectoryMembershipProvider” type=”System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
connectionStringName=”ADConnectionString” attributeMapUsername=”sAMAccountName” enableSearchMethods=”true”/>
<add name=”AspNetSqlMembershipProvider” type=”System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” connectionStringName=”LocalSqlServer” />
</providers>
</membership>

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

Using two ASP.NET membership providers…

This post has moved to http://castnerit.com/blog/?p=5...

Leave a comment

(required)

(required)