Shining Star Services LLC

Creating a Custom Membership Provider and Membership User utilizing a Data Set Table Adapter - Step 7

by Nannette Thacker

Creating Our Custom MembershipUser Class

We are now ready to create our custom MembershipUser Class. First create a new class and add it to your ProviderClasses directory.



Add "Inherits MembershipUser."


I've commented my code with further suggested readings on this topic, as well as certain "gotchas."

ChangePassword in MembershipUser versus MembershipProvider

The system automatically calls the MembershipProvider "ChangePassword" method first, so your MembershipProvider method will never be implemented unless you call it in this method as below:

#Region "ChangePassword Functionality"
        ' http://devauthority.com/blogs/jwooley/archive/2006/08/25/2208.aspx
        ' system automatically calls this one first, since the membershipuser has its 
        ' own method, use this to call the membershipprovider changepassword...
        Public Overrides Function ChangePassword(ByVal oldPassword As String, _
        ByVal newPassword As String) As Boolean
            ' Validate the old password for user 
            If Membership.Provider.ValidateUser(Me.UserName, oldPassword) Then
                Return Membership.Provider.ChangePassword(Me.UserName, _
                oldPassword, newPassword)
            Else ' Incorrect username/password combination.
                Return False
            End If
        End Function

#End Region
 
The complete MembershipUser class is below:

Imports Microsoft.VisualBasic

Namespace ShiningStar

    Public Class SSSMembershipUser
        Inherits MembershipUser


#Region "Notes"
        ' http://msdn2.microsoft.com/en-us/ms366730.aspx
        ' implementing a custom membership user
#End Region

#Region "New() Construction"


        Public Sub New(ByVal userName As String)
            ' http://devauthority.com/blogs/jwooley/archive/2006/08/25/2208.aspx
            ' for changepassword to work below... values don't matter...
            MyBase.New("Shiningstar.SSSMembershipProvider", userName, _
            userName.GetHashCode, userName, "", "", False, False, Today, _
            Today, Today, Today, Today)

            Me.m_UserName = userName ' UserName() is read only

        End Sub


        ' 11/20/07 change membershipprovider type to string.... nkt

        Public Sub New(ByVal membershipProvider As String, ByVal userID As Decimal, _
        ByVal UserName As String)
            ' Check Readonly Non-Nullable Property Values
            If userID = Nothing Then _
                Throw New ArgumentNullException("UserID is null.")
            If String.IsNullOrEmpty(UserName) Then _
                Throw New ArgumentNullException("UserName is null.")
            If String.IsNullOrEmpty(membershipProvider) Then _
                Throw New ArgumentNullException("membershipProvider is null.")

            ' Set Readonly Property Values
            Me.m_MembershipProvider = membershipProvider
            Me.m_UserID = userID ' Member() is read only
            Me.m_UserName = UserName ' UserName() is read only

        End Sub


        Public Sub New(ByVal membershipProvider As String, ByVal userID As Decimal, _
            ByVal UserName As String, ByVal UserEmail As String, ByVal UserPassword As String, _
            ByVal CreateDate As Date, ByVal LastActivityDate As Date, ByVal IPAddress As String)

            Me.New(membershipProvider, userID, UserName)

            ' Set Property Values
            Me.Email = UserEmail
            Me.Password = UserPassword
            Me.IPAddress = IPAddress
            Me.CreationDate = CreateDate
            Me.LastActivityDate = LastActivityDate
            Me.LastLoginDate = LastActivityDate

        End Sub

#End Region

#Region "Variables"

        Private m_MembershipProvider As String
        Private m_UserID As Decimal
        Private m_UserName As String
        Private m_UserEmail As String
        Private m_UserPassword As String
        Private m_IPAddress As String
        Private m_CreationDate As System.Nullable(Of Date) = Nothing
        Private m_LastLoginDate As System.Nullable(Of Date) = Nothing

        ' not used
        Private m_IsLockedOut As Boolean = False
        Private m_IsApproved As Boolean = False
        Private Shared m_UserNameMaxLength As Integer = 20

#End Region

        Public ReadOnly Property MembershipProvider() As String
            Get
                Return m_MembershipProvider
            End Get
        End Property

#Region "Properties Stored in Database"

        Public ReadOnly Property UserID() As Decimal
            Get
                Return m_UserID
            End Get
        End Property

        Public Overrides ReadOnly Property UserName() As String
            Get
                Return m_UserName
            End Get
        End Property

        Public Overrides Property Email() As String
            Get
                Return m_UserEmail
            End Get
            Set(ByVal value As String)
                If String.IsNullOrEmpty(value) Then _
                    Throw New ArgumentNullException("User Email is null.")

                If Not String.Equals(m_UserEmail, value) Then
                    m_UserEmail = value
                End If
            End Set
        End Property

        ' use this property to set a member's password if the format is "clear"
        ' this will result in no encryption
        Public Property Password() As String
            Get
                Return m_UserPassword
            End Get
            Set(ByVal value As String)
                If String.IsNullOrEmpty(value) Then _
                    Throw New ArgumentNullException("Password is null.")

                If Not String.Equals(m_UserPassword, value) Then
                    m_UserPassword = value
                End If
            End Set
        End Property

        Public Property IPAddress() As String
            Get
                Return m_IPAddress
            End Get
            Set(ByVal value As String)
                If String.IsNullOrEmpty(value) Then _
                    Throw New ArgumentNullException("IPAddress is null.")

                If Not String.Equals(m_IPAddress, value) Then
                    m_IPAddress = value
                End If
            End Set
        End Property

        Public Shadows Property CreationDate() As System.Nullable(Of Date)
            Get
                Return m_CreationDate
            End Get
            Set(ByVal value As System.Nullable(Of Date))
                If m_CreationDate.HasValue And value.HasValue Then
                    If Not m_CreationDate.Value.Equals(value.Value) Then
                        m_CreationDate = value
                    End If
                ElseIf (Not m_CreationDate.HasValue And value.HasValue) Or _
                (m_CreationDate.HasValue And Not value.HasValue) Then
                    m_CreationDate = value
                End If
            End Set
        End Property

        Public Shadows Property LastLoginDate() As System.Nullable(Of Date)
            Get
                Return m_LastLoginDate
            End Get
            Set(ByVal value As System.Nullable(Of Date))
                If m_LastLoginDate.HasValue And value.HasValue Then
                    If Not m_LastLoginDate.Value.Equals(value.Value) Then
                        m_LastLoginDate = value
                    End If
                ElseIf (Not m_LastLoginDate.HasValue And value.HasValue) Or _
                (m_LastLoginDate.HasValue And Not value.HasValue) Then
                    m_LastLoginDate = value
                End If
            End Set
        End Property

#End Region

#Region "Properties Not Used by Us"

        Public Shadows Property IsLockedOut() As Boolean
            Get
                Return m_IsLockedOut
            End Get
            Set(ByVal value As Boolean)
                If value = Nothing Then _
                    value = False

                If Not Boolean.Equals(m_IsLockedOut, value) Then
                    m_IsLockedOut = value
                End If
            End Set
        End Property

        Public Overrides Property IsApproved() As Boolean
            Get
                Return m_IsApproved
            End Get
            Set(ByVal value As Boolean)
                If value = Nothing Then _
                    value = False

                If Not Boolean.Equals(m_IsApproved, value) Then
                    m_IsApproved = value
                End If
            End Set
        End Property

        Public Shared ReadOnly Property UserNameMaxLength() As Integer
            Get
                Return m_UserNameMaxLength
            End Get
        End Property

#End Region

#Region "Password Functionality"

#Region "ChangePassword Functionality"
        ' http://devauthority.com/blogs/jwooley/archive/2006/08/25/2208.aspx
        ' system automatically calls this one first, since the membershipuser has its 
        ' own method, use this to call the membershipprovider changepassword...
        Public Overrides Function ChangePassword(ByVal oldPassword As String, _
        ByVal newPassword As String) As Boolean
            ' Validate the old password for user 
            If Membership.Provider.ValidateUser(Me.UserName, oldPassword) Then
                Return Membership.Provider.ChangePassword(Me.UserName, oldPassword, _
                newPassword)
            Else ' Incorrect username/password combination.
                Return False
            End If
        End Function

#End Region

#Region "GetPassword Functionality"

        Public Overrides Function GetPassword() As String
            Return Membership.Provider.GetPassword(Me.UserName, String.Empty)
        End Function

        Public Overrides Function GetPassword(ByVal passwordAnswer As String) As String
            Return Membership.Provider.GetPassword(Me.UserName, passwordAnswer)
        End Function

#End Region

#End Region

    End Class

End Namespace

We are now ready to begin Step 8: Step 8: Customizing our MembershipProvider Class..

Steps for Creating a Custom Membership Provider and Membership User utilizing a DataSet Table Adapter:

Introduction: Creating a Custom Membership Provider and Membership User utilizing a DataSet Table Adapter.
Step 1: Creating the Project and Folders.
Step 2: Table Data Structure and Web.config for this Tutorial.
Step 3: Creating The DataSet.
Step 4: Creating the Table Adapter Methods for GetUserByLogin, GetUserByUserName, and InsertUser.
Step 5: Creating our Custom MembershipProvider Class.
Step 6: Adding Properties to Our Custom MembershipProvider Class.
Step 7: Creating Our Custom MembershipUser Class.
Step 8: Customizing our MembershipProvider Class.
Step 9: Our Custom RoleProvider Class.
Step 10: Our Master page.
Step 11: Our Cookie Handler class.
Step 12: Our Log In page.
Step 13: Our Register page.
Step 14: Our Log Out page.
Step 15: Our Change Password page.
Step 16: Our Administration page.
Step 17: Loading a Menu Programmatically based on Roles.
Step 18: Our Default page.
Download the ZIP files:
VB: ShiningStarCustomMemberProviderTutorial.zip
© Copyright 1997-2017 Shining Star Services LLC, Nannette Thacker. All Rights Reserved.