locked
Search Group for User RRS feed

  • Question

  • User-44640224 posted
    I am trying to create a login that will authenticate against Active Directory.  I have the login working but I need to have it return some information so that I can use it later on in the website.  I need to check 3 different groups in active directory to determine which group the user is in.  I also need to get the CN and email for the user.  The user supplies their username and password.  I found the code below for authenticating the username and password.  Now I just need to add to it or create a new function to get the rest of the information.  Can someone point me in the right direction (preferably VB code)?  Thanks for all the help in advance.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal Username As String, ByVal Password As String) As Boolean

    Dim Success As Boolean = False

    Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, Username, Password)Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)

    Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel

    Try

    Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne

    Success = Not (Results Is Nothing)

    Catch

    Success = False

    End Try

    Return Success

    End Function

     

     

    Friday, July 20, 2007 11:52 AM

Answers

  • User-44640224 posted

    I figured out how to get what I was looking for by doing the following.  Hope this helps anyone else tring to do the same thing.

    Public Function GetMemberOf(ByVal username As String, ByVal pwd As String) As String

    Dim strMemberof As String

    Dim propertyCount As Integer

    Dim dn As String

    Dim equalsIndex As String

    Dim commaIndex As String

     

    pwd = Nothing ' works better if pwd is nothing

    Dim domain As String = DomainNameValue

    Dim path As String = "LDAP://" + domain

    Dim domainAndUsername As String = domain + "\" + username

    Dim entry As DirectoryEntry = New DirectoryEntry(path, domainAndUsername, pwd)

    Dim Searcher As DirectorySearcher = New DirectorySearcher(entry)

    Dim result As System.DirectoryServices.SearchResult

    Searcher.PropertiesToLoad.Add("memberof")Searcher.Filter = ("(anr=" & username & ")")

    result = Searcher.FindOne()

    If Not IsNothing(result) Then

    propertyCount = result.Properties("memberof").Count

    For i As Integer = 0 To propertyCount - 1

    dn = result.Properties("memberOf")(i)

    equalsIndex = dn.IndexOf("=", 1)

    commaIndex = dn.IndexOf(",", 1)

    If equalsIndex = -1 Then

    Return Nothing

    End If

    strMemberof = dn.Substring((equalsIndex + 1), _

    (commaIndex - equalsIndex) - 1)

    If strMemberof = "Group1" Then

    Session("Company") = "Products"

    Exit For

    ElseIf strMemberof = "Group2" Or strMemberof = "Group3" Then

    Session("Company") = "Services"

    Exit For

    End If

    Next

    Else

    strMemberof = "Does Not Belong To Products or Services."

    End If

    Return strMemberof

    End Function

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2007 10:29 AM