Categories: Active Directory Posted on 5/20/2009 3:30 AM by Ryan Shelby  Feedback (5)

A few days ago I showed how to read security policies and password complexity properties from Active Directory using VB.NET.  In this post I will talk about a class that encapulates the Active Directory User Object.

 

There are a lot of user properties in Active Directory, and most of these can be retrieved using .NET.

 

 

Although there are probably many more properties, the example below shows how to encapsulate a user from Active Directory with the following properties:

  • Given Name
  • Last Name
  • Middle Initial
  • Display Name
  • Email (or Mail)
  • Proxy Address
  • Title
  • Department
  • Company
  • Physical Delivery Office Name
  • Telephone Number
  • Mobile (Phone)
  • Facs Number
  • Street Address
  • City
  • State
  • Postal Code (Zip Code)
  • Country
  • Distinguished Name
  • MemberOf (Array List of AD Group Object)

 

Each public property in the ADUser Class passes its official AD name to a private function called GetUserProperty.  If the user property in Active Directory is empty, then string value "No Information" is returned.

 

    1 Imports System.DirectoryServices
    2 Imports System.Configuration.ConfigurationManager
    3 
    4 Namespace ActiveDirectory
    5 
    6     Public Class ADUser
    7 
    8 #Region "Private Property Variables"
    9 
   10         'Class objects
   11         Private m_entry As DirectoryEntry
   12         Private m_search As DirectorySearcher
   13 
   14         'Personal
   15         Private m_username As String
   16         Private m_givenname As String       'First Name
   17         Private m_sn As String              'Last Name
   18         Private m_middleinitial As String        'Middle Initial
   19         Private m_displayname As String     'Full Name
   20         Private m_samaccountname As String
   21         Private m_distinguishedname As String
   22         Private m_email As String            'Email
   23         Private m_proxyaddresses As String
   24         Private m_memberof As ArrayList
   25 
   26         'Office
   27         Private m_title As String
   28         Private m_department As String
   29         Private m_company As String
   30         Private m_physicaldeliveryofficename As String
   31         Private m_telephonenumber As String
   32         Private m_mobile As String
   33         Private m_facsimiletelephonenumber As String
   34 
   35         'Address
   36         Private m_streetaddress As String   'Street Address
   37         Private m_l As String   'City
   38         Private m_st As String  'State
   39         Private m_postalcode As String  'Zip Code
   40         Private m_co As String  'Country
   41 
   42 #End Region
   43 
   44 #Region "Public Properties"
   45         Public Property UserName() As String
   46             Get
   47                 Return m_username
   48             End Get
   49             Set(ByVal value As String)
   50                 m_username = value
   51             End Set
   52         End Property
   53         Public ReadOnly Property GivenName() As String
   54             Get
   55                 m_givenname = GetUserProperty("givenname")
   56                 Return m_givenname
   57             End Get
   58         End Property
   59         Public ReadOnly Property LastName() As String
   60             Get
   61                 m_sn = GetUserProperty("sn")
   62                 Return m_sn
   63             End Get
   64         End Property
   65         Public ReadOnly Property MiddleInitial() As String
   66             Get
   67                 m_middleinitial = GetUserProperty("initials")
   68                 Return m_middleinitial
   69             End Get
   70         End Property
   71         Public ReadOnly Property DisplayName() As String
   72             Get
   73                 m_displayname = GetUserProperty("displayname")
   74                 Return m_displayname
   75             End Get
   76         End Property
   77         Public ReadOnly Property EMail() As String
   78             Get
   79                 m_email = GetUserProperty("mail")
   80                 Return m_email
   81             End Get
   82         End Property
   83         Public ReadOnly Property ProxyAddresses() As String
   84             Get
   85                 m_proxyaddresses = GetUserProperty("proxyaddresses")
   86                 Return m_proxyaddresses
   87             End Get
   88         End Property
   89         Public ReadOnly Property Title() As String
   90             Get
   91                 m_title = GetUserProperty("title")
   92                 Return m_title
   93             End Get
   94         End Property
   95         Public ReadOnly Property Department() As String
   96             Get
   97                 m_department = GetUserProperty("department")
   98                 Return m_department
   99             End Get
  100         End Property
  101         Public ReadOnly Property Company() As String
  102             Get
  103                 m_company = GetUserProperty("company")
  104                 Return m_company
  105             End Get
  106         End Property
  107         Public ReadOnly Property PhysicalDeliveryOfficeName() As String
  108             Get
  109                 m_physicaldeliveryofficename = GetUserProperty("physicaldeliveryofficename")
  110                 Return m_physicaldeliveryofficename
  111             End Get
  112         End Property
  113         Public ReadOnly Property TelephoneNumber() As String
  114             Get
  115                 m_telephonenumber = GetUserProperty("telephonenumber")
  116                 Return m_telephonenumber
  117             End Get
  118         End Property
  119         Public ReadOnly Property Mobile() As String
  120             Get
  121                 m_mobile = GetUserProperty("mobile")
  122                 Return m_mobile
  123             End Get
  124         End Property
  125         Public ReadOnly Property FacsimileTelephoneNumber() As String
  126             Get
  127                 m_facsimiletelephonenumber = GetUserProperty("facsimiletelephonenumber")
  128                 Return m_facsimiletelephonenumber
  129             End Get
  130         End Property
  131         Public ReadOnly Property StreetAddress() As String
  132             Get
  133                 m_streetaddress = GetUserProperty("streetaddress")
  134                 Return m_streetaddress
  135             End Get
  136         End Property
  137         Public ReadOnly Property City() As String
  138             Get
  139                 m_l = GetUserProperty("l")
  140                 Return m_l
  141             End Get
  142         End Property
  143         Public ReadOnly Property State() As String
  144             Get
  145                 m_st = GetUserProperty("st")
  146                 Return m_st
  147             End Get
  148         End Property
  149         Public ReadOnly Property PostalCode() As String
  150             Get
  151                 m_postalcode = GetUserProperty("postalcode")
  152                 Return m_postalcode
  153             End Get
  154         End Property
  155         Public ReadOnly Property Country() As String
  156             Get
  157                 m_co = GetUserProperty("co")
  158                 Return m_co
  159             End Get
  160         End Property
  161         Public ReadOnly Property DistinguishedName() As String
  162             Get
  163                 m_distinguishedname = GetUserProperty("DistinguishedName")
  164                 Return m_distinguishedname
  165             End Get
  166         End Property
  167         Public Property MemberOf() As ArrayList
  168             Get
  169                 m_memberof = ADGroup.LoadGroupsByUserName(DistinguishedName)
  170                 Return m_memberof
  171             End Get
  172             Set(ByVal Value As ArrayList)
  173                 m_memberof = Value
  174             End Set
  175         End Property
  176 #End Region
  177 
  178         Public Sub New()
  179         End Sub
  180 
  181         Public Sub New(ByVal user_name As String, Optional ByVal FilterName As String = "SAMAccountName")
  182 
  183             m_username = user_name
  184             m_samaccountname = user_name
  185 
  186             'Get connection string, username and password to connect to Active Directory.
  187             Dim ADConnString As String = ConnectionStrings("ADConnString").ConnectionString
  188             Dim ADUserName As String = AppSettings("ADUserName")
  189             Dim ADPassword As String = AppSettings("ADPassword")
  190 
  191             'Start Directory Entry.
  192             m_entry = New DirectoryEntry(ADConnString, ADUserName, ADPassword)
  193 
  194             m_search = New DirectorySearcher(m_entry)
  195             m_search.Filter = "(" & FilterName & "=" & m_samaccountname & ")"
  196 
  197         End Sub
  198 
  199         Private Function GetUserProperty(ByVal PropName As String) As String
  200 
  201             m_search.PropertiesToLoad.Add(PropName)
  202             Dim result As SearchResult = m_search.FindOne()
  203 
  204             If result.Properties(PropName).Count > 0 Then
  205                 Return result.Properties(PropName)(0).ToString()
  206             Else
  207                 Return "No Information"
  208             End If
  209 
  210         End Function
  211 
  212     End Class
  213 
  214 End Namespace

 

 

Next time I will go over the Active Directory Group Object and its LoadGroupsByUserName function.

 

Comments

Adt security
Adt security on 8/30/2009 5:46 PM Thanks for the simple explanation. I wish there was a way to make the fields required so we have more information to retrieve. Too much "No information".
Plastic injection molding
Plastic injection molding on 9/19/2009 1:41 PM Thanks for sharing the info, and you have given it a very explanatory article.
Predictive dialer
Predictive dialer on 9/30/2009 8:36 AM Thanks for the updated info as u said ,there are probably many more properties,but  the example that you have  shown how to encapsulate a user from Active Directory with  properties did the job for me,keep posting more
article
article on 10/13/2009 11:18 AM Thanks for the info i love reading your blog
Rolex mens datejust
Rolex mens datejust on 10/29/2009 8:22 PM Yes, there are already a lot of user properties in Active Directory. How can I add my own? I mean for a customized application.

Send Feedback





biuquote
  • Comment
  • Preview
Loading