Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Saturday, July 04, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

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.

Related Articles
   Related Document Methods for Active Directory Management


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 10/16/2008 5:09:24 AM by Anonymous
Comments: simple, clear, to the point
Left on 9/26/2008 10:55:29 PM by Anonymous
Comments: poor

No ratings available.
Left on 3/22/2007 8:08:15 AM by Anonymous
Comments:
Left on 9/7/2006 10:32:41 AM by Anonymous
Comments: why find all?  what about find one?  you normally dont change EVERY person in AD all at once.  you normally work with only 1 user at a time.  this is dumb code
Left on 8/17/2006 10:09:03 PM by Anonymous
Comments: very helpful with what i need to do. thanks.
No ratings available.
Left on 7/26/2006 8:42:45 AM by Anonymous
Comments: Article is good and informative for the users. Can you please tell me how can we assign rights to the users in ldap server, like one group can have access to other group and etc....
my mailid : rbsalunkhe@yahoo.co.in

Please help me in this regarding
No ratings available.
Left on 7/26/2006 5:00:08 AM by Anonymous
Comments: hello im trying to use this code but i don't know where to put these functions n declerations. plz can any bdy send me the complete code at itsrajiv@hotmail.com
will be a great help
No ratings available.
Left on 7/20/2006 5:08:59 AM by Anonymous
Comments: "A referral was returned from the server" -- please check if you have specified all correct DC= in the LDAP string.
No ratings available.
Left on 7/15/2006 9:40:42 AM by Anonymous
Comments: THANK YOU
No ratings available.
Left on 6/3/2006 5:51:45 PM by Anonymous
Comments: hi i have active i need some help this is my code:
Public Function IsAuthenticated(ByVal domain As String, ByVal username As String, ByVal pwd As String) As Boolean

            Dim domainAndUsername As String = username + " @ " + domain
            Dim entry As DirectoryEntry = New DirectoryEntry(_path, domainAndUsername, pwd)
and my path is Dim adPath as String = "ldap://ldap://192.168.1.6:389/CN=Administrateurs,CN=Users,,DC=SIR"
but it give ma an error 0*000080... somthing like that so if any one could help me its an emergency

No ratings available.
Left on 5/31/2006 8:45:05 AM by Anonymous
Comments: Very good stuff and very usefull thanks man i m become your fan now
No ratings available.
Left on 5/16/2006 8:23:44 AM by Anonymous
Comments: Nice artical, this helped me a lot... Thanks
Left on 5/9/2006 9:53:50 AM by Anonymous
Comments: Nice example
No ratings available.
Left on 3/7/2006 9:20:52 AM by Anonymous
Comments: what is the property of the user class in Active Directory suppose to be? is it persons? i am trying access employees info
No ratings available.
Left on 2/20/2006 1:43:42 PM by Anonymous
Comments: never mind
No ratings available.
Left on 2/20/2006 10:36:20 AM by Anonymous
Comments: Where is UserAccount define? help

No ratings available.
Left on 11/16/2005 4:04:33 PM by Anonymous
Comments: how you set the filter to use DN attribute instead of Samaccountname ?

Thanks
No ratings available.
Left on 6/23/2005 2:43:37 AM by Anonymous
Comments: provide example for the domain path.
LDAP://yourdomainpath.com/DC=yourdomainpath,DC=com

otherwise ok.
No ratings available.
Left on 6/16/2005 4:55:03 AM by Anonymous
Comments: New DirectoryEntry(sPath, "Enterprise Admin", "Password") helped me!
Thanks!!!
Left on 6/14/2005 12:32:54 AM by Anonymous
Comments: Nice example;  getting error though: "A referral was returned from the server"   Any ideas?

Left on 5/26/2005 2:15:55 AM by Anonymous
Comments: I was trying from my local account but when I deploy it to the server it was not able to retrive the name. But after your code, when I wrote the username and pw of an admin(me) now I can retrive it..THANKS A LOT!
No ratings available.
Left on 5/25/2005 3:18:47 AM by Anonymous
Comments: Exccellence!!! Love you..
No ratings available.
Left on 5/8/2005 1:52:50 PM by Anonymous
Comments: how to you set the "inType" and UserAccount varibles and when to you set them ?
No ratings available.
Left on 2/16/2005 9:55:21 AM by Anonymous
Comments: Solid, sraight to the point. and nicely worked example.
Left on 2/1/2005 9:42:45 AM by Anonymous
Comments: can anyone provide a full example.. as im a complete noob
Left on 1/31/2005 5:39:49 PM by Anonymous
Comments: How about posting the source code in zip format?
Left on 12/28/2004 2:59:24 PM by Anonymous
Comments: Great! Found exactly what I was looking for.
Left on 10/20/2004 11:39:36 PM by Anonymous
Comments: I try this but error says, Right, Len, InStr, UserAccount not declared
No ratings available.
Left on 10/7/2004 10:32:48 AM by Anonymous
Comments: Great article!  By adding to the web.config, one can avoid hardcoding the login and password in the DirectoryEntry(sPath) ;)
No ratings available.
Left on 10/7/2004 10:04:30 AM by Anonymous
Comments: Awesome!  You solved my problem!  Great job!
No ratings available.
Left on 9/9/2004 1:30:06 AM by Anonymous
Comments: How to add user in AD using LDAP path
No ratings available.
Left on 6/28/2004 9:16:37 AM by Anonymous
Comments: Works fantastic. Thanks
Left on 6/22/2004 8:56:11 AM by Anonymous
Comments: Works right out of the box, and gives a much better idea of how to do this than anything else I have found on the web so far.
Left on 6/16/2004 10:56:27 AM by Anonymous
Comments: Very thanks !
Left on 6/3/2004 2:59:15 PM by Anonymous
Comments: Incredibly helpful!  Thanks for posting this.
Left on 6/2/2004 10:06:00 AM by Anonymous
Comments: Is there a way to tell if a user is currently logged in anywhere in the AD?  (eMail me at keith@indigotechnologies.us)
No ratings available.
Left on 5/28/2004 4:47:30 PM by Anonymous
Comments: Great code... One bad thing. How can I do this without using the Enterprise Admin account. What permissions does the user account actually require. Simple browse access in AD?
No ratings available.
Left on 5/12/2004 7:58:53 AM by Anonymous
Comments: Please, give me an example of domainpath as the following..
Dim sPath As String =  "LDAP://yourdomainpath.com/DC=yourdomainpath,DC=com"

No ratings available.
Left on 5/10/2004 2:06:43 PM by Anonymous
Comments: GetUserInfo(UserAccount, "telephoneNumber")

No ratings available.
Left on 4/19/2004 11:00:17 AM by Anonymous
Comments: This article should provide you with group lookups: http://www.wwwcoder.com/main/parentid/272/site/2208/68/default.aspx
No ratings available.
Left on 4/4/2004 8:20:59 AM by Anonymous
Comments: How we could access to special group??
Left on 3/25/2004 7:44:58 AM by Anonymous
Comments: By all means, you rule.
Left on 3/5/2004 4:03:02 PM by Anonymous
Comments: Useless for beginner.  This is the same code posted everywhere.
Left on 2/6/2004 5:27:13 AM by Anonymous
Comments: no se capisse un casso
No ratings available.
Left on 1/14/2004 3:44:18 PM by Anonymous
Comments: Goddam, this makes my life easy. 
Left on 12/23/2003 2:27:16 PM by Anonymous
Comments: i have searched high and low for some code i could get to work. thanks a ton!
Left on 12/16/2003 6:13:53 PM by Anonymous
Comments: Good for beginner
Left on 8/25/2003 5:45:28 PM by Anonymous
Comments: Great example. Nice to know that Net extends the ADSI legacy.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970