Add ADSync Test Project, ActiveDirectoryInterface

This commit is contained in:
Jonathan Jenne
2019-04-01 16:49:24 +02:00
parent 05f5e730e1
commit 7ebd07cf14
24 changed files with 850 additions and 7 deletions

View File

@@ -0,0 +1,124 @@
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

View File

@@ -0,0 +1,13 @@
Public Class ADGroup
Public SAMAccountName As String
Public ObjectClass As String
Public CN As String
Public Description As String
Public DistinguishedName As String
Public Name As String
Public ObjectCategory As String
Public Overrides Function ToString() As String
Return $"SAMAccountName={SAMAccountName}, ObjectClass={ObjectClass}, CN={CN}, Description={Description}, DistinguishedName={DistinguishedName}, Name={Name}, ObjectCategory={ObjectCategory}"
End Function
End Class

View File

@@ -50,6 +50,7 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.DirectoryServices" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
@@ -72,7 +73,9 @@
<Import Include="System.Threading.Tasks" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions.vb" />
<Compile Include="ActiveDirectoryInterface.vb" />
<Compile Include="ActiveDirectoryInterface\ActiveDirectoryGroup.vb" />
<Compile Include="ZUGFeRDInterface\Exceptions.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
@@ -88,7 +91,7 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="CrossIndustryDocumentType.vb" />
<Compile Include="ZUGFeRDInterface\CrossIndustryDocumentType.vb" />
<Compile Include="ZUGFeRDInterface.vb" />
</ItemGroup>
<ItemGroup>
@@ -110,7 +113,7 @@
<LastGenOutput>Settings.Designer.vb</LastGenOutput>
</None>
<None Include="packages.config" />
<None Include="pdf_zugferd_lib.lib">
<None Include="ZUGFeRDInterface\pdf_zugferd_lib.lib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
@@ -121,10 +124,10 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="pdf_zugferd_lib.dll">
<Content Include="ZUGFeRDInterface\pdf_zugferd_lib.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="pdf_zugferd_test.exe">
<Content Include="ZUGFeRDInterface\pdf_zugferd_test.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

View File

@@ -9,7 +9,7 @@ Public Class ZUGFeRDInterface
Private _logConfig As LogConfig
Private _logger As Logger
Private Const ZUGFERD_CONVERTER_EXE = "pdf_zugferd_test.exe"
Private Const ZUGFERD_CONVERTER_EXE = "ZUGFeRDInterface\pdf_zugferd_test.exe"
Private Const ZUGFERD_CONVERTER_SUCCESS_MESSAGE = "Document contains ZUGFeRD data."
Public Enum ErrorType

View File

@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="IKVM" version="8.0.5449.1" targetFramework="net461" />
<package id="NLog" version="4.5.11" targetFramework="net461" />
</packages>