Obtaining Active Directory User Information Using System.DirectoryServices
6/12/2003 1:17:34 PM
In this article we cover using the System.DirectoryServices and obtaining information on an Active Directory user account in an Active Directory domain.
In this article we're going to provide a quick example of accessing Active Directory using the System.DirectoryServices of .Net. In previous articles we went over using ADSI to make calls to Active Directory in your ASP or Windows Scripting Host application. Using the System.DirectoryServices class you can accomplish the same thing in your .Net applications.
Imports System Imports System.Security.Principal Imports System.DirectoryServices Imports System.Web
Public Class ActiveDirectory
In the following function we will pass the Sam account name to the function. This can be obtained by reference the Context.User.Indentity.Name object in .Net. or by referring to the Request.Servervariables("LOGON_USER") object. The second value passed to the function is the property of the user class in Active Directory that you want to get a value of.
Public Function GetUserInfo(ByVal inSAM As String, ByVal inType As String) As String Try Dim sPath As String = "LDAP://yourdomainpath.com/DC=yourdomainpath,DC=com" Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\")) Dim myDirectory As New DirectoryEntry(sPath, "Enterprise Admin", "Password") 'pass the user account and password for your Enterprise admin. Dim mySearcher As New DirectorySearcher(myDirectory) Dim mySearchResultColl As SearchResultCollection Dim mySearchResult As SearchResult Dim myResultPropColl As ResultPropertyCollection Dim myResultPropValueColl As ResultPropertyValueCollection 'Build LDAP query mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))") mySearchResultColl = mySearcher.FindAll() 'I expect only one user from search result Select Case mySearchResultColl.Count Case 0 Return "Null" Exit Function Case Is > 1 Return "Null" Exit Function End Select 'Get the search result from the collection mySearchResult = mySearchResultColl.Item(0)
'Get the Properites, they contain the usefull info myResultPropColl = mySearchResult.Properties
'displayname, mail 'Retrieve from the properties collection the display name and email of the user myResultPropValueColl = myResultPropColl.Item(inType) Return CStr(myResultPropValueColl.Item(0))
Catch ex As System.Exception
'do some error return here. End Try End Function
Here we will just create some variables and populate them with the call to the GetUserInfo function by passing the user account and name of the property we want to get.
Dim sEmail As String = GetUserInfo(UserAccount, "mail") Dim sFirstName As String = GetUserInfo(UserAccount, "givenName") Dim sLastName As String = GetUserInfo(UserAccount, "sn") Dim sCity As String = GetUserInfo(UserAccount, "l") Dim sState As String = GetUserInfo(UserAccount, "st") Dim sStreetAddress As String = GetUserInfo(UserAccount, "streetAddress") Dim sPostalCode As String = GetUserInfo(UserAccount, "postalCode") Dim sPhone As String = GetUserInfo(UserAccount, "telephoneNumber") Dim sCountry As String = GetUserInfo(UserAccount, "co") :: :: :: :: End Class
You can use this code as a starting point for obtaining information on users in your Active Directory domain.
By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.
|