Shining Star Services LLC

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

by Nannette Thacker

Our Custom RoleProvider Class

Below is our source code from our customized RoleProvider class:

Imports Microsoft.VisualBasic

Namespace ShiningStar

    Public Class SSSRoleProvider
        Inherits RoleProvider

#Region "URLS"

        'http://odetocode.com/Articles/428.aspx
        ' restrict locations of the web application to specific roles using settings in web.config
        '<system.web>
        '  <authorization>
        '    <allow roles="Admin"/>
        '    <deny users="*"/>
        '  </authorization>
        '</system.web>

        'granular authorization checks by using IsUserInRole
        ' If Roles.IsUserInRole("Admin") Then

        ' declaratively demand a user to belong to a specific role
        '<PrincipalPermission(SecurityAction.Demand, Role:="Registered")> _
        ' Protected Sub DoSomethingImportant()

        ' http://geekswithblogs.net/Shadowin/archive/2007/04/05/110864.aspx
        ' Pitfall while building custom role provider
        ' http://msdn2.microsoft.com/en-us/library/ms998314.aspx
        ' How To: Use Role Manager in ASP.NET 2.0
        ' http://msdn2.microsoft.com/en-us/library/tksy7hd7.aspx
        ' Sample Role-Provider Implementation

        'http://flimflan.com/blog/ASPNETRoleProviderVisualStudioTemplate.aspx

#End Region



#Region "Role Functions Not used yet"

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

            End Set
        End Property

        Public Overrides Sub CreateRole(ByVal roleName As String)

        End Sub

        Public Overrides Function DeleteRole(ByVal roleName As String, ByVal throwOnPopulatedRole As Boolean) As Boolean

        End Function

        Public Overrides Function FindUsersInRole(ByVal roleName As String, ByVal usernameToMatch As String) As String()
            Return Split("")
        End Function

        Public Overrides Function GetAllRoles() As String()
            Return Split("")
        End Function

        Public Overrides Function GetUsersInRole(ByVal roleName As String) As String()
            Return Split("")
        End Function

        Public Overrides Sub RemoveUsersFromRoles(ByVal usernames() As String, ByVal roleNames() As String)

        End Sub

        Public Overrides Function RoleExists(ByVal roleName As String) As Boolean

        End Function

        Public Overrides Sub AddUsersToRoles(ByVal usernames() As String, ByVal roleNames() As String)

        End Sub

#End Region

#Region "comments"
        'http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/GetDataFromDB.aspx
#End Region

        Public Overrides Function GetRolesForUser(ByVal userName As String) As String()

            ' nkt: this is also used by Principal.IsInRole for the logged in user instead of IsUserInRole

            Dim myArray As String = ""
            Dim IAdapter As New SSNetDataSetTableAdapters.UserRolesTableAdapter
            Dim myRoles As SSNetDataSet.UserRolesDataTable = Nothing
            myRoles = IAdapter.GetUserRolesByUserName(userName)

            For Each myRolesRow As SSNetDataSet.UserRolesRow In myRoles
                myArray = myArray & myRolesRow.roleShort.ToString
            Next

            Return Split(myArray)

        End Function



        Public Overrides Function IsUserInRole(ByVal userName As String, ByVal roleName As String) As Boolean

            ' nkt: this can throw you for a loop, so be sure to read it
            ' use this only if checking for the NON-LOGGED IN user.
            ' otherwise, the system automatically calls Principal.IsInRole
            ' http://www.thescripts.com/forum/thread470503.html
            ' RolePrincipal uses the GetRolesForUser to cache the roles and do the role
            ' checking among the cached role list.

            For Each role As String In Roles.GetRolesForUser(userName)
                If role = roleName Then
                    Return True
                End If
            Next
            Return False

        End Function


    End Class


#Region "Wrapper Class for roles"

    Public Class SSSRole

        Shared Function IsRoleInArray(ByVal roleArray As String(), ByVal roleName As String) As Boolean
            For Each role As String In roleArray
                If CStr(role) = CStr(roleName) Then
                    Return True
                End If
            Next
            Return False
        End Function

    End Class


#End Region

End Namespace
We are now ready to begin Step 10: Step 10: Our Master page.

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.