Link to home
Start Free TrialLog in
Avatar of Kevin Robinson
Kevin Robinson

asked on

IsInRole is returning false

This is part of my security class.  but when i call the is in role function it always return false.  The current user (administrator) is a member of the VBIGrantsAdministrator group in active directory.
When i show message box of what strings are being return it looks OK.



 Private Function GetUsername() As String

        Dim Principal As System.Security.Principal.IPrincipal = Nothing
        Principal = System.Threading.Thread.CurrentPrincipal

        Dim User As String
        User = Principal.Identity.Name
        Return User

    End Function

 Private Function GetDomain() As String
        Dim Domain As String = GetUsername().Substring(0, GetUsername().IndexOf("\") + 1)
        Return Domain
    End Function

    Private Function GetIsInRole() As Boolean
        Return System.Threading.Thread.CurrentPrincipal.IsInRole(Me.GetDomain & "VBIGrantsAdministrator")
    End Function
ASKER CERTIFIED SOLUTION
Avatar of TSmooth
TSmooth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin Robinson
Kevin Robinson

ASKER

Imports System.Security
Imports System.Threading


Public Class Authentication

    Public Enum Group
        CVSGrantsAdministrator
        VBIGrantsAdministrator
        MVGrantsAdministrator
    End Enum


    Function IsInRole(ByVal Group As Group) As Boolean

        Dim ReturnValue As Boolean = False

        Select Case Group
            Case Authentication.Group.CVSGrantsAdministrator
                ReturnValue = Me.CheckRole("CVSGrantsAdministrator")
            Case Authentication.Group.MVGrantsAdministrator
                ReturnValue = Me.CheckRole("MVGrantsAdministrator")
            Case Authentication.Group.VBIGrantsAdministrator
                ReturnValue = Me.CheckRole("VBIGrantsAdministrator")
        End Select

        Return ReturnValue

    End Function



    Private Function CheckRole(ByVal Group As String) As Boolean
        Dim MyPrincipal As System.Security.Principal.WindowsPrincipal

        ' set the stage by picking type of security principal
        AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal)
        ' get the security principal for this thread
        MyPrincipal = CType(System.Threading.Thread.CurrentPrincipal, System.Security.Principal.WindowsPrincipal)

        If MyPrincipal.IsInRole(Group) Then
            Return True
        Else
            Return False
        End If
    End Function


End Class