#Region " Imports " Imports Statistics.AppException Imports Statistics.conversion Imports System Imports System.Collections.Generic Imports System.DirectoryServices Imports System.Text #End Region Namespace Data Public Class ActiveDirectory #Region " Instance Fields " Shared _ADRoot As String ' The point at which to begin searching the AD Shared _city As String = String.Empty Shared _company As String = String.Empty Shared _displayName As String = String.Empty Shared _dsn As String = String.Empty Shared _email As String = String.Empty Shared _employeeID As String = String.Empty Shared _firstName As String = String.Empty Shared _fullName As String = String.Empty Shared _lastName As String = String.Empty Shared _jobTitle As String = String.Empty Shared _ldap As String = String.Empty Shared _middleName As String = String.Empty Shared _officeSymbol As String = String.Empty Shared _organization As String = String.Empty Shared _phone As String = String.Empty Shared _state As String = String.Empty Shared _street As String = String.Empty Shared _title As String = String.Empty Shared _userName As String = String.Empty Shared _webPageAddress As String = String.Empty Shared _zipCode As String = "99999" #End Region #Region " Properties " Public Shared Property activeDirectoryRoot() As String Get Return _ADRoot End Get Set(ByVal value As String) _ADRoot = value End Set End Property Public Shared ReadOnly Property city() As String Get Return _city End Get End Property Public Shared ReadOnly Property company() As String Get Return _company End Get End Property Public Shared ReadOnly Property displayName() As String Get Return _displayName End Get End Property Public Shared ReadOnly Property dsn() As String Get If Not _dsn = String.Empty Then Return Conversion.PhoneNumber(_dsn) Else Return Conversion.PhoneNumber(_phone) End If End Get End Property Public Shared ReadOnly Property edipi() As String Get Return _employeeID End Get End Property Public Shared ReadOnly Property email() As String Get Return _email End Get End Property Public Shared ReadOnly Property firstName() As String Get Return _firstName End Get End Property Public Shared ReadOnly Property fullName() As String Get Return GetFullName() End Get End Property Public Shared ReadOnly Property jobTitle() As String Get Return _jobTitle End Get End Property Public Shared ReadOnly Property lastName() As String Get Return _lastName End Get End Property Public Shared ReadOnly Property ldap() As String Get Return _ldap End Get End Property Public Shared ReadOnly Property middleName() As String Get Return _middleName End Get End Property Public Shared ReadOnly Property officeSymbol() As String Get Return _officeSymbol End Get End Property Public Shared ReadOnly Property organization() As String Get Return _organization End Get End Property Public Shared ReadOnly Property phone() As String Get If Not _phone = String.Empty Then Return Conversion.PhoneNumber(_phone) Else Return Conversion.PhoneNumber(_dsn) End If End Get End Property Public Shared ReadOnly Property state() As String Get Return _state End Get End Property Public Shared ReadOnly Property street() As String Get Return _street End Get End Property Public Shared ReadOnly Property title() As String Get Return _title End Get End Property Public Shared ReadOnly Property userName() As String Get Return _userName End Get End Property Public Shared ReadOnly Property webPageAddress() As String Get Return _webPageAddress End Get End Property Public Shared ReadOnly Property zipCode() As String Get Return _zipCode End Get End Property #End Region #Region " Constructor " 'Construct with default root: Public Sub New() _ADRoot = Nothing End Sub 'Construct with specified root: ' First argument "activeDirectoryRoot" specifies where to start looking. Public Sub New(ByVal activeDirectoryRoot As String) _ADRoot = activeDirectoryRoot End Sub #End Region #Region " Public Methods " 'Method resets properties: Public Shared Sub Clear() _city = String.Empty _company = String.Empty _displayName = String.Empty _dsn = String.Empty _email = String.Empty _employeeID = String.Empty _firstName = String.Empty _fullName = String.Empty _lastName = String.Empty _jobTitle = String.Empty _ldap = String.Empty _middleName = String.Empty _officeSymbol = String.Empty _organization = String.Empty _phone = String.Empty _state = String.Empty _street = String.Empty _title = String.Empty _userName = String.Empty _webPageAddress = String.Empty _zipCode = "99999" End Sub 'Method tests for existance of specified user by email address: ' First argument "emailAddress" specifies the user's SMTP address. ' Returns true if the user exists, false otherwise. Public Shared Function EmailExists(ByVal emailAddress As String) As Boolean Dim returnValue As SearchResult Dim searchRoot As New DirectoryEntry(_ADRoot) Dim filter As String = "(mail=" & emailAddress & ")" Dim ds As DirectorySearcher = New DirectorySearcher(searchRoot, filter) Try returnValue = ds.FindOne() Catch ex As Exception Throw New AppException("ERROR AD4 - Unable to locate an email entry in Active Directory: ", ex) End Try If Not returnValue Is Nothing Then Return True Else Return False End If End Function 'Method seeks and returns active directory object for specified user: ' First argument "edipi" specifies the user's EDIPI number. ' Returns the user's directory entry object. Public Shared Function GetADUser(ByVal edipi As String) As DirectoryEntry Dim returnValue As SearchResult Dim searchRoot As New DirectoryEntry(_ADRoot) Dim filter As String = "(userPrincipalName=" & edipi & "@mil)" Dim ds As DirectorySearcher = New DirectorySearcher(searchRoot, filter) Try returnValue = ds.FindOne() Catch ex As Exception Throw New AppException("ERROR AD2 - Unable to locate an edipi entry in Active Directory: ", ex) End Try Return returnValue.GetDirectoryEntry End Function 'Method seeks and returns active directory object for specified user by email address: ' First argument "emailAddress" specifies the user's SMTP address. ' Returns the user's directory entry object. Public Shared Function GetADUserByEmail(ByVal emailAddress As String) As DirectoryEntry Dim returnValue As SearchResult Dim searchRoot As New DirectoryEntry(_ADRoot) Dim filter As String = "(mail=" & emailAddress & ")" Dim ds As DirectorySearcher = New DirectorySearcher(searchRoot, filter) Try returnValue = ds.FindOne() Catch ex As Exception Throw New AppException("ERROR AD3 - Unable to locate an email entry in Active Directory: ", ex) End Try If Not returnValue Is Nothing Then Return returnValue.GetDirectoryEntry 'Return found DirectoryEntry Else Return searchRoot 'Return empty DirectoryEntry if not found. End If End Function 'Method populates class properties from Active Directory 'Directory Entry object: ' First argument "de" specifies the AD object to process. Public Shared Sub Initialize(ByVal de As DirectoryEntry) Try If Not de.Properties("l").Value Is Nothing Then _city = CStr(de.Properties("l").Value) End If If Not de.Properties("company").Value Is Nothing Then _company = CStr(de.Properties("company").Value) End If If Not de.Properties("displayName").Value Is Nothing Then _displayName = CStr(de.Properties("displayName").Value) End If If Not de.Properties("telephoneNumber").Value Is Nothing Then _dsn = CStr(de.Properties("telephoneNumber").Value) End If If Not de.Properties("mail").Value Is Nothing Then _email = CStr(de.Properties("mail").Value) End If If Not de.Properties("employeeID").Value Is Nothing Then _employeeID = CStr(de.Properties("employeeID").Value) End If If Not de.Properties("givenName").Value Is Nothing Then _firstName = StrConv(CStr(de.Properties("givenName").Value), VbStrConv.ProperCase) End If If Not de.Properties("Title").Value Is Nothing Then _jobTitle = CStr(de.Properties("Title").Value) End If If Not de.Properties("sn").Value Is Nothing Then _lastName = StrConv(CStr(de.Properties("sn").Value), VbStrConv.ProperCase) End If _ldap = CStr(de.Path) If Not de.Properties("middleName").Value Is Nothing Then _middleName = StrConv(CStr(de.Properties("middleName").Value), VbStrConv.ProperCase) End If If Not de.Properties("physicalDeliveryOfficeName").Value Is Nothing Then _officeSymbol = CStr(de.Properties("physicalDeliveryOfficeName").Value) End If If Not de.Properties("department").Value Is Nothing Then _organization = CStr(de.Properties("department").Value) End If If Not de.Properties("otherTelephone").Value Is Nothing Then _phone = CStr(de.Properties("otherTelephone").Value) End If If Not de.Properties("st").Value Is Nothing Then _state = CStr(de.Properties("st").Value) End If If Not de.Properties("streetAddress").Value Is Nothing Then _street = CStr(de.Properties("streetAddress").Value) End If If Not de.Properties("personaltitle").Value Is Nothing Then _title = CStr(de.Properties("personaltitle").Value) End If If Not de.Properties("SAMAccountName").Value Is Nothing Then _userName = CStr(de.Properties("SAMAccountName").Value) End If If Not de.Properties("WWWHomePage").Value Is Nothing Then _webPageAddress = CStr(de.Properties("WWWHomePage").Value) End If If Not de.Properties("postalCode").Value Is Nothing Then _zipCode = CStr(de.Properties("postalCode").Value) End If Catch ex As Exception Throw New AppException("ERROR AD1 - Error reading one of the properties on Active Directory Entry: ", ex) End Try End Sub #End Region #Region " Private Methods " 'Method returns a construction of the requested full name: ' Returns the full name in a string Private Shared Function GetFullName() As String If _middleName = String.Empty Then GetFullName = _firstName & " " & _lastName Else GetFullName = _firstName & " " & _middleName & ". " & _lastName End If End Function #End Region End Class End Namespace