Active Directory Access (in Global.aspx)

Import Namespace="System.DirectoryServices"
Import Namespace="System.Web.HttpRequest"

 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        Application("ApplicationName") = "MyAppName"
        Dim sessionData As New Dictionary(Of String, HttpSessionState)()
        Application("s") = sessionData
    End Sub
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
    
      Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
        Dim sessionData As Dictionary(Of String, HttpSessionState) = DirectCast(Application("s"), Dictionary(Of String, HttpSessionState))
    
        If sessionData.Keys.Contains(HttpContext.Current.Session.SessionID) Then
            sessionData.Remove(HttpContext.Current.Session.SessionID)
            sessionData.Add(HttpContext.Current.Session.SessionID, HttpContext.Current.Session)
        Else
            sessionData.Add(HttpContext.Current.Session.SessionID, HttpContext.Current.Session)
        End If
        Application("s") = sessionData
        Session("username") = HttpContext.Current.User.Identity.Name
        Dim user As String = ExtractUserName(Session("userName"))
        Session("userid") = user
        Dim list As String
        list = GetADUserGroups(user)
        
        Dim groups() As String
        groups = list.Split("|")
        
        Dim i As Integer
        For i = 0 To groups.GetUpperBound(0)
            If Not groups(i).Length = 0 Then
                If groups(i).Substring(3, 14) = "Administrators" Then
                    Session("Admin") = groups(i).Substring(3, 14)
                ElseIf groups(i).Substring(3, 7) = "Editors" Then
                    Session("Editor") = groups(i).Substring(3, 7)
                ElseIf groups(i).Substring(3, 7) = "Viewers" Then
                    Session("Viewer") = groups(i).Substring(3, 7)
                End If
            End If
        Next
    End Sub
    Public Shared Function ExtractUserName(ByVal path As String) As String
        'Split on the "\"
        Dim userPath As String() = path.Split(New Char() {"\"c})
        'Return the rest (username part)
        Return userPath((userPath.Length - 1))
    End Function
    
    Private Function GetADUserGroups(ByVal userName As String) As String
        Dim search As New DirectorySearcher()
        search.Filter = [String].Format("(sAMAccountName={0})", userName)
        search.PropertiesToLoad.Add("memberOf")
        Dim groupsList As New StringBuilder()
	    
        Dim result As SearchResult = search.FindOne()
        If result IsNot Nothing Then
            Dim groupCount As Integer = result.Properties("memberOf").Count
	        
            For counter As Integer = 0 To groupCount - 1
                groupsList.Append(DirectCast(result.Properties("memberOf")(counter), String))
                groupsList.Append("|")
            Next
	    End If 
        Return (groupsList.ToString())
    End Function
       
    Private Function GetGroups(ByVal _path As String, ByVal username As String, ByVal password As String) As Collection
        Dim Groups As New Collection
        Dim dirEntry As New System.DirectoryServices.DirectoryEntry(_path, username, password)
        Dim dirSearcher As New DirectorySearcher(dirEntry)
        dirSearcher.Filter = String.Format("(sAMAccountName={0}))", username)
        dirSearcher.PropertiesToLoad.Add("memberOf")
        Dim propCount As Integer
        Try
            Dim dirSearchResults As SearchResult = dirSearcher.FindOne()
            propCount = dirSearchResults.Properties("memberOf").Count
            Dim dn As String
            Dim equalsIndex As String
            Dim commaIndex As String
            For i As Integer = 0 To propCount - 1
                dn = dirSearchResults.Properties("memberOf")(i)
                equalsIndex = dn.IndexOf("=", 1)
                commaIndex = dn.IndexOf(",", 1)
                If equalsIndex = -1 Then
                    Return Nothing
                End If
                If Not Groups.Contains(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)) Then
                    Groups.Add(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1))
                End If
            Next
        Catch ex As Exception
            If ex.GetType Is GetType(System.NullReferenceException) Then
                Response.Write("Selected user isn't a member of any groups at this time")
            Else
                Response.Write(ex.Message.ToString())
            End If
        End Try
        Return Groups
    End Function