Sure, basically when you use those default asp.net membership and role tables in sql server, the code you are really using is the default sql server membership provider. Since it's the default you don't have to write any code other than configuring in your web.config that you are using it.
If you want to use your own database objects, obviously the default sql membership provider would have no idea what your table and field names are, what your stored procedure names and arguments are, etc. So you have to write a custom membership provider that inherits from System.Web.Security.Membership provider, and implement all of the desired functionalities that it provides.
For example, obviously one of the key functionalities it provides is authenticating a user. You have to override a function named ValidateUser. In there you provide the custom code you would use to validate a user with whatever is stored in your database.
Code:
Imports Microsoft.VisualBasic
Imports System.Web.Security
Imports System.Data.SqlClient
Imports System.Configuration
Public Class CustomMembershipProvider
Inherits System.Web.Security.MembershipProvider
Public Overrides Property ApplicationName() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
Public Overrides Function ChangePassword(ByVal username As String, ByVal oldPassword As String, ByVal newPassword As String) As Boolean
End Function
Public Overrides Function ChangePasswordQuestionAndAnswer(ByVal username As String, ByVal password As String, ByVal newPasswordQuestion As String, ByVal newPasswordAnswer As String) As Boolean
End Function
Public Overrides Function CreateUser(ByVal username As String, ByVal password As String, ByVal email As String, ByVal passwordQuestion As String, ByVal passwordAnswer As String, ByVal isApproved As Boolean, ByVal providerUserKey As Object, ByRef status As System.Web.Security.MembershipCreateStatus) As System.Web.Security.MembershipUser
End Function
Public Overrides Function DeleteUser(ByVal username As String, ByVal deleteAllRelatedData As Boolean) As Boolean
End Function
Public Overrides ReadOnly Property EnablePasswordReset() As Boolean
Get
End Get
End Property
Public Overrides ReadOnly Property EnablePasswordRetrieval() As Boolean
Get
End Get
End Property
Public Overrides Function FindUsersByEmail(ByVal emailToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection
End Function
Public Overrides Function FindUsersByName(ByVal usernameToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection
End Function
Public Overrides Function GetAllUsers(ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection
End Function
Public Overrides Function GetNumberOfUsersOnline() As Integer
End Function
Public Overrides Function GetPassword(ByVal username As String, ByVal answer As String) As String
End Function
Public Overloads Overrides Function GetUser(ByVal providerUserKey As Object, ByVal userIsOnline As Boolean) As System.Web.Security.MembershipUser
End Function
Public Overloads Overrides Function GetUser(ByVal username As String, ByVal userIsOnline As Boolean) As System.Web.Security.MembershipUser
End Function
Public Overrides Function GetUserNameByEmail(ByVal email As String) As String
End Function
Public Overrides ReadOnly Property MaxInvalidPasswordAttempts() As Integer
Get
End Get
End Property
Public Overrides ReadOnly Property MinRequiredNonAlphanumericCharacters() As Integer
Get
End Get
End Property
Public Overrides ReadOnly Property MinRequiredPasswordLength() As Integer
Get
End Get
End Property
Public Overrides ReadOnly Property PasswordAttemptWindow() As Integer
Get
End Get
End Property
Public Overrides ReadOnly Property PasswordFormat() As System.Web.Security.MembershipPasswordFormat
Get
End Get
End Property
Public Overrides ReadOnly Property PasswordStrengthRegularExpression() As String
Get
End Get
End Property
Public Overrides ReadOnly Property RequiresQuestionAndAnswer() As Boolean
Get
End Get
End Property
Public Overrides ReadOnly Property RequiresUniqueEmail() As Boolean
Get
End Get
End Property
Public Overrides Function ResetPassword(ByVal username As String, ByVal answer As String) As String
End Function
Public Overrides Function UnlockUser(ByVal userName As String) As Boolean
End Function
Public Overrides Sub UpdateUser(ByVal user As System.Web.Security.MembershipUser)
End Sub
Public Overrides Function ValidateUser(ByVal username As String, ByVal password As String) As Boolean
Dim sqlCon As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DbConnectionString").ToString())
Dim sqlCmd As SqlCommand = New SqlCommand( _
"SELECT 1 " & _
"FROM User " & _
"WHERE userId = @userName AND LEN(userId) > 0 " & _
"AND password = @password ", sqlCon)
sqlCmd.Parameters.Add(New SqlParameter("@userName", System.Data.SqlDbType.VarChar))
sqlCmd.Parameters("@userName").Value = username
sqlCmd.Parameters.Add(New SqlParameter("@password", System.Data.SqlDbType.VarChar))
sqlCmd.Parameters("@password").Value = password
sqlCon.Open()
Dim returnVal As Boolean = False
If CStr(sqlCmd.ExecuteScalar()) = "1" Then
returnVal = True
End If
sqlCon.Close()
Return returnVal
End Function
End Class
When you inherit your class from System.Web.Security.MembershipProvider, Visual Studio will automatically insert all of the properties and function signatures you can implement, and it's your job to implement the ones you want. I provided an example of implementing the ValidateUser which is obviously the most important. Good luck.