125 lines
4.1 KiB
VB.net
125 lines
4.1 KiB
VB.net
Imports System.DirectoryServices
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class ActiveDirectoryInterface
|
|
Private _logConfig As LogConfig
|
|
Private _logger As Logger
|
|
|
|
Private _rootPath As String
|
|
Private _rootNode As DirectoryEntry
|
|
|
|
Private Const SEARCH_LIMIT = 50000
|
|
|
|
Private Const SAMACCOUNTNAME = "samaccountname"
|
|
Private Const OBJECTCLASS = "objectClass"
|
|
Private Const CN = "cn"
|
|
Private Const DESCRIPTION = "description"
|
|
Private Const DISINGUISHEDNAME = "distinguishedName"
|
|
Private Const NAME = "name"
|
|
Private Const OBJECTCATEGORY = "objectCategory"
|
|
|
|
Public Sub New(LogConfig As LogConfig, Optional RootPath As String = Nothing)
|
|
_logConfig = LogConfig
|
|
_logger = _logConfig.GetLogger()
|
|
If RootPath Is Nothing Then
|
|
_rootPath = $"LDAP://{Environment.UserDomainName}"
|
|
Else
|
|
_rootPath = RootPath
|
|
End If
|
|
End Sub
|
|
|
|
Private Function GetRootNode() As DirectoryEntry
|
|
Dim oEntry As New DirectoryEntry(_rootPath) With {
|
|
.AuthenticationType = AuthenticationTypes.Secure,
|
|
.Password = Nothing,
|
|
.Username = Nothing
|
|
}
|
|
|
|
Return oEntry
|
|
End Function
|
|
Private Function GetRootNode(Username As String, Password As String) As DirectoryEntry
|
|
Dim oEntry As New DirectoryEntry(_rootPath) With {
|
|
.AuthenticationType = AuthenticationTypes.Secure,
|
|
.Password = Username,
|
|
.Username = Password
|
|
}
|
|
|
|
Return oEntry
|
|
End Function
|
|
|
|
Public Sub Authenticate()
|
|
Try
|
|
Dim oEntry = GetRootNode()
|
|
oEntry.RefreshCache()
|
|
|
|
_rootNode = oEntry
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
_logger.Warn("Could not authenticate with Active Directory.")
|
|
End Try
|
|
End Sub
|
|
Public Sub Authenticate(Username As String, Password As String)
|
|
Try
|
|
Dim oEntry = GetRootNode(Username, Password)
|
|
oEntry.RefreshCache()
|
|
|
|
_rootNode = oEntry
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
_logger.Warn("Could not authenticate with Active Directory.")
|
|
End Try
|
|
End Sub
|
|
|
|
Public Function ListGroups() As List(Of ADGroup)
|
|
Return ListGroups(_rootNode)
|
|
End Function
|
|
|
|
Public Function ListGroups(RootNode As DirectoryEntry) As List(Of ADGroup)
|
|
Dim oGroups As New List(Of ADGroup)
|
|
|
|
Try
|
|
Dim oFilterQuery As String = "(&(objectClass=group) (samAccountName=*))"
|
|
Dim oDirectorySearcher As New DirectorySearcher(RootNode) With {
|
|
.SearchScope = SearchScope.Subtree,
|
|
.SizeLimit = SEARCH_LIMIT,
|
|
.Filter = oFilterQuery
|
|
}
|
|
Dim oResults As SearchResultCollection = oDirectorySearcher.FindAll()
|
|
|
|
_logger.Info("Found {0} Groups.", oResults.Count)
|
|
|
|
Return GroupResultsToList(oResults)
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
Return oGroups
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GroupResultsToList(Results As SearchResultCollection) As List(Of ADGroup)
|
|
Dim oGroups As New List(Of ADGroup)
|
|
|
|
For Each oResult As SearchResult In Results
|
|
oGroups.Add(New ADGroup() With {
|
|
.Name = TryGetProperty(oResult, NAME),
|
|
.SAMAccountName = TryGetProperty(oResult, SAMACCOUNTNAME),
|
|
.CN = TryGetProperty(oResult, CN),
|
|
.Description = TryGetProperty(oResult, DESCRIPTION),
|
|
.DistinguishedName = TryGetProperty(oResult, DISINGUISHEDNAME),
|
|
.ObjectCategory = TryGetProperty(oResult, OBJECTCATEGORY),
|
|
.ObjectClass = TryGetProperty(oResult, OBJECTCLASS)
|
|
})
|
|
Next
|
|
|
|
Return oGroups
|
|
End Function
|
|
|
|
Private Function TryGetProperty(Result As SearchResult, PropertyName As String) As String
|
|
Try
|
|
Return Result.Properties.Item(PropertyName).Item(0)
|
|
Catch ex As Exception
|
|
_logger.Warn("Property {0} not found")
|
|
Return String.Empty
|
|
End Try
|
|
End Function
|
|
End Class
|