Click here to Skip to main content
15,884,473 members
Articles / Web Development / ASP.NET

Developing custom ASP.NET Membership and Role providers reading users from custom section in the web.config

Rate me:
Please Sign up or sign in to vote.
4.76/5 (33 votes)
7 Aug 2008CPOL8 min read 127.4K   2.1K   74  
In this article, we will be developing custom Membership and Role providers which will read user credentials from a custom configuration section in the web.config file.
Imports Configuration
Namespace AspNetProvider
    Public Class WebConfigMembershipProvider
        Inherits MembershipProvider

        Private _ApplicationName As String

        Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)
            '===retrives the attribute values set in
            'web.config and assign to local variables===
            _ApplicationName = config("ApplicationName")
            MyBase.Initialize(name, config)
        End Sub

        Public Overrides Property ApplicationName() As String
            Get
                Return _ApplicationName
            End Get
            Set(ByVal value As String)
                _ApplicationName = value
            End Set
        End Property

        Public Overrides Function ChangePassword(ByVal username As String, ByVal oldPassword As String, ByVal newPassword As String) As Boolean
            Throw New System.NotSupportedException("No saving ")
        End Function

        Public Overrides Function ChangePasswordQuestionAndAnswer(ByVal username As String, ByVal password As String, ByVal newPasswordQuestion As String, ByVal newPasswordAnswer As String) As Boolean
            Throw New System.NotSupportedException("Not implemented")
        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
            Throw New System.NotSupportedException("Not implemented")
        End Function

        Public Overrides Function DeleteUser(ByVal username As String, ByVal deleteAllRelatedData As Boolean) As Boolean
            Throw New System.NotSupportedException("Not implemented")
        End Function

        Public Overrides ReadOnly Property EnablePasswordReset() As Boolean
            Get
                Return False
            End Get
        End Property

        Public Overrides ReadOnly Property EnablePasswordRetrieval() As Boolean
            Get
                Return False
            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
            Dim output As New System.Web.Security.MembershipUserCollection
            For Each user As CustomUser In UsersConfigurationSection.Current.Users
                If user.Email.Equals(emailToMatch) Then
                    output.Add(user.AspNetMembership(Me.Name))
                End If
            Next
            Return output
        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
            Dim output As New System.Web.Security.MembershipUserCollection
            For Each user As CustomUser In UsersConfigurationSection.Current.Users
                If user.UserName.Equals(usernameToMatch) Then
                    output.Add(user.AspNetMembership(Me.Name))
                End If
            Next
            Return output
        End Function

        Public Overrides Function GetAllUsers(ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Security.MembershipUserCollection
            Dim output As New System.Web.Security.MembershipUserCollection
            For Each user As CustomUser In UsersConfigurationSection.Current.Users
                output.Add(user.AspNetMembership(Me.Name))
            Next
            Return output
        End Function

        Public Overrides Function GetNumberOfUsersOnline() As Integer
            Throw New System.NotSupportedException("No saving ")
        End Function

        Public Overrides Function GetPassword(ByVal username As String, ByVal answer As String) As String
            Throw New System.NotSupportedException("The question/answer model is not supported yet!")
        End Function

        Public Overloads Overrides Function GetUser(ByVal providerUserKey As Object, ByVal userIsOnline As Boolean) As System.Web.Security.MembershipUser
            Dim output As System.Web.Security.MembershipUser = Nothing
            For Each user As CustomUser In UsersConfigurationSection.Current.Users
                If user.UserName.Equals(providerUserKey) Then
                    output = user.AspNetMembership(Me.Name)
                End If
            Next
            Return output
        End Function

        Public Overloads Overrides Function GetUser(ByVal username As String, ByVal userIsOnline As Boolean) As System.Web.Security.MembershipUser
            Dim output As System.Web.Security.MembershipUser = Nothing
            For Each user As CustomUser In UsersConfigurationSection.Current.Users
                If user.UserName.Equals(username) Then
                    output = user.AspNetMembership(Me.Name)
                End If
            Next
            Return output
        End Function

        Public Overrides Function GetUserNameByEmail(ByVal email As String) As String
            Dim output As String = Nothing
            For Each user As CustomUser In UsersConfigurationSection.Current.Users
                If user.Email.Equals(email) Then
                    output = user.UserName
                End If
            Next
            Return output
        End Function

        Public Overrides ReadOnly Property MaxInvalidPasswordAttempts() As Integer
            Get
                Return 5
            End Get
        End Property

        Public Overrides ReadOnly Property MinRequiredNonAlphanumericCharacters() As Integer
            Get
                Return 0
            End Get
        End Property

        Public Overrides ReadOnly Property MinRequiredPasswordLength() As Integer
            Get
                Return 8
            End Get
        End Property

        Public Overrides ReadOnly Property PasswordAttemptWindow() As Integer
            Get
                Return 30
            End Get
        End Property

        Public Overrides ReadOnly Property PasswordFormat() As System.Web.Security.MembershipPasswordFormat
            Get
                Return MembershipPasswordFormat.Clear
            End Get
        End Property

        Public Overrides ReadOnly Property PasswordStrengthRegularExpression() As String
            Get
                Return ""
            End Get
        End Property

        Public Overrides ReadOnly Property RequiresQuestionAndAnswer() As Boolean
            Get
                Return False
            End Get
        End Property

        Public Overrides ReadOnly Property RequiresUniqueEmail() As Boolean
            Get
                Return False
            End Get
        End Property

        Public Overrides Function ResetPassword(ByVal username As String, ByVal answer As String) As String
            Throw New System.NotSupportedException("No saving")
        End Function

        Public Overrides Function UnlockUser(ByVal userName As String) As Boolean
            Throw New System.NotSupportedException("No saving")
        End Function

        Public Overrides Sub UpdateUser(ByVal user As System.Web.Security.MembershipUser)
            Throw New System.NotSupportedException("No saving")
        End Sub

        Public Overrides Function ValidateUser(ByVal username As String, ByVal password As String) As Boolean
            Dim output As Boolean = False
            If username.Length > 0 Then
                Dim myUser As CustomUser = UsersConfigurationSection.Current.Users.Item(username)
                If myUser IsNot Nothing Then
                    output = myUser.Password.Equals(password)
                End If
            End If
            Return output
        End Function
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions