Membership API with Windows Authentication and a Custom Role Provider - ASP.NET

Overview

If you manage users in your own database table, but still want to use the ASP.NET Membership API to control access to pages, you will need to create a custom role provider. This article describes how to implement one. The key to this is overriding the GetRolesForUser method, which is called when ASP.NET checks if a user has authorization to view a page.

Procedure

Web.Config

<configuration>
    <system.web>
        . . .
        <authentication mode="Windows"/>
        <roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="MyRoleProvider">
            <providers>
                <add name="MyRoleProvider"
                         type="MyNamespace.RoleProvider"
                         />
            </providers>

        </roleManager>
        <authorization>
            <allow users="*" />
            <deny users="?" />
        </authorization>
    </system.web>
    <location path="Admin">
        <system.web>
            <authorization>
                <allow roles="ADM"/>
                <deny users="*"/>
                <deny users="?"/>
            </authorization>
        </system.web>
    </location>
    . . .
</configuration>

RoleProvider Class

VB.NET

Namespace MyNamespace
    Public Class RoleProvider
    Inherits System.Web.Security.RoleProvider

        Public Overrides Function GetRolesForUser(ByVal username As String) As String()
            ' TODO: Implement this method
            Dim result As String() = {}
            Return result
        End Function


        ' These remaining methods are necessary because they are declared MustInherit in the base class.
        Public Overrides Sub AddUsersToRoles(ByVal usernames() As String, ByVal roleNames() As String)

        End Sub

        Public Overrides Property ApplicationName() As String
            Get
                Return "MyApp"
            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
            Return False
        End Function

        Public Overrides Function FindUsersInRole(ByVal roleName As String, ByVal usernameToMatch As String) As String()
            Dim result As String() = {}
            Return result
        End Function

        Public Overrides Function GetAllRoles() As String()
            Dim result As String() = {}
            Return result
        End Function

        Public Overrides Function GetUsersInRole(ByVal roleName As String) As String()
            Dim result As String() = {}
            Return result
        End Function

        Public Overrides Function IsUserInRole(ByVal username As String, ByVal roleName As String) As Boolean
            Return True
        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
            Return True
        End Function
    End Class
End Namespace

C#

//TODO