Call

Tag Archives: AD

VB.NET: Retrieving Active Directory account properties

0
Filed under ASP.NET, Microsoft

The .NET Framework includes an API called System.DirectoryServices. The sample VB.NET code below shows how to retrieve user account properties from the Active Directory.

In order to use System.DirectoryServices classes, you will need to first add a reference to System.DirectoryServices.

Imports System.DirectoryServices is required at the very top of your VB.NET code.

Function GetUserProperties(ByVal GetUser, ByVal Result)
        Dim objSearch As New DirectorySearcher
        objSearch.SearchRoot = New DirectoryEntry("LDAP://dc=domain,dc=co,dc=uk")
        objSearch.Filter = GetUser
        'cn = Full Name, givenName = First name, initials = Middle names, sn = Surname, mail = email address
        objSearch.PropertiesToLoad.AddRange(New String() {"cn", "giveName", "initials", "sn", "department", "mail", "Manager"})
        Dim objResult As SearchResult
        objResult = objSearch.FindOne()
        GetUserProperties = (objResult.Properties(Result)(0))
End Function

Replace dc=domain, dc=co, dc=uk with your AD settings.