Pages

Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Monday, July 4, 2011

.NET function to get user groups from Active Directory

Below .NET function is used to get the user groups from Active Directory.

To use the below function in your project, you need to add the reference for System.DirectoryServices


Public Function GetUserGroupMembership(ByVal strUser As String) As StringCollection  Dim groups As New StringCollection()
Try
  Dim obEntry As New DirectoryServices.DirectoryEntry("
LDAP://domain/DC=domain,DC=com")
  Dim srch As New DirectoryServices.DirectorySearcher(obEntry, "(sAMAccountName=" & strUser & ")")
  Dim res As DirectoryServices.SearchResult = srch.FindOne()
      If res IsNot Nothing Then
        Dim obUser As New DirectoryServices.DirectoryEntry(res.Path)
        ' Invoke Groups method.
        Dim obGroups As Object = obUser.Invoke("Groups")
        For Each ob As Object In DirectCast(obGroups, IEnumerable)
        ' Create object for each group.
        Dim obGpEntry As New DirectoryServices.DirectoryEntry(ob)
                    groups.Add(obGpEntry.Name)
        Next
      End If
Catch ex As Exception            'Trace.Write(ex.Message)
End Try
        Return groups
End Function



Above function can be called as :
Dim Groups As StringCollection = GetUserGroupMembership(username.text)

.NET function to get user information from Active Directory

Below script is a .NET function to get information about a user from AD. This information can be stored in a variable and used in your project/application.

To use the below function in your project, you need to add the reference for System.DirectoryServices


Public Function GetUserInfo(ByVal inAD As String, ByVal inType As String) As String
Try
    Dim sPath As String= "
LDAP://domain/DC=domain,DC=com"
    Dim SamAccount As String = Right(inAD, Len(inAD) - InStr(inAD, "\"))
    Dim myDirectory As New DirectoryServices.DirectoryEntry(sPath, "username", "Password")
    Dim mySearcher As New DirectoryServices.DirectorySearcher(myDirectory)
    Dim mySearchResultColl As DirectoryServices.SearchResultCollection
    Dim mySearchResult As DirectoryServices.SearchResult
    Dim myResultPropColl As DirectoryServices.ResultPropertyCollection
    Dim myResultPropValueColl As DirectoryServices.ResultPropertyValueCollection
    'Build LDAP query
    mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
    mySearchResultColl = mySearcher.FindAll()
    'only one user from search result
            Select Case mySearchResultColl.Count
                Case 0
                    Return "0"
                    Exit Function
                Case Is > 1
                    Return "1"
                    Exit Function
            End Select

            'Search result from the collection
            mySearchResult = mySearchResultColl.Item(0)
           
     'Properites, they contain the usefull info
            myResultPropColl = mySearchResult.Properties
            myResultPropValueColl = myResultPropColl.Item(inType)
            Return CStr(myResultPropValueColl.Item(0))


Catch ex As System.Exception           
    Return 0
End Try
End Function

Above function can be called as below:
Variablename = GetUserInfo(usernametexbox.text, "displayname")
where displayname is the property name in Active Directory