ToolCollection/ToolCollection/ClassActiveDirectory.vb
Digital Data - Marlon Schreiber 3a25947af0 MS
2019-02-05 18:49:13 +01:00

178 lines
8.2 KiB
VB.net

Imports System.DirectoryServices
Imports DigitalData.Modules.Logging
Public Class ClassActiveDirectory
Public Shared licenseManager As ClassLicenseManager = Nothing
Public Shared ErgebnisAD As String()
Private Shared _Logger As DigitalData.Modules.Logging.Logger
Public Shared Function Test_Connect(SEARCH_Filter As String, Property2Load As String, Domain As String, User As String, PW As String)
Try
Dim objDirectoryEntry As DirectoryEntry = New DirectoryEntry(Domain)
If PW <> String.Empty And User <> String.Empty Then
objDirectoryEntry.Username = User
objDirectoryEntry.Password = PW
End If
'Always use a secure connection
objDirectoryEntry.AuthenticationType = AuthenticationTypes.Secure
Dim searcher As New DirectorySearcher() 'rootEntry
searcher.SearchRoot = objDirectoryEntry 'New DirectoryEntry(Domain)
' get the LDAP filter string based on selections on the form
searcher.PropertyNamesOnly = True
searcher.PropertiesToLoad.Add(Property2Load)
'would also work and saves you some code
searcher.Filter = SEARCH_Filter
Dim results As SearchResultCollection
results = searcher.FindAll()
Dim result As SearchResult
Dim i As Integer = 0
Dim AttributValues As New ArrayList
For Each result In results
If result.GetDirectoryEntry.Properties.Item(Property2Load).Value Is Nothing = False Then
AttributValues.Add(result.GetDirectoryEntry.Properties.Item(Property2Load).Value)
i += 1
'Einfahc nur um nicht alles zu durchlaufen
If i = 50 Then
Exit For
End If
'Else
' MsgBox("nothing")
End If
Next
Return AttributValues
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler in Test_Connect LDAP:")
End Try
End Function
Public Shared Function GetAD_Attribut(SEARCH_Filter As String, Property2Load As String, Domain As String, User As String, PW As String, Optional test As Boolean = False)
Try
If SEARCH_Filter.Contains("SUBSTR[") Then
Dim temp_Filter As String
Dim temp_area As String
' Regulären Ausdruck zum Auslesen der windream-Indexe definieren
Dim preg As String = "(?:SUBSTR\[)([\s\S]*),(\d{1,2},\d{1,2})(?:\])"
' einen Regulären Ausdruck laden
Dim regulärerAusdruck As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(preg)
' die Vorkommen im SQL-String auslesen
Dim elemente As System.Text.RegularExpressions.MatchCollection = regulärerAusdruck.Matches(SEARCH_Filter)
' alle Vorkommen der windream-Indexe im SQL-String durchlaufen
For Each element As System.Text.RegularExpressions.Match In elemente
For g As Integer = 1 To element.Groups.Count - 1
If g = 1 Then
temp_Filter = element.Groups(g).Value
If temp_Filter.Contains("*") Then
temp_Filter = temp_Filter.Replace("*", "")
End If
If temp_Filter.Contains("%") Then
temp_Filter = temp_Filter.Replace("%", "")
End If
ElseIf g = 2 Then
temp_area = element.Groups(g).Value
'Begin/End herausfinden
Dim split() = temp_area.Split(",")
If split.Length = 2 Then
Dim newsubstr As String = temp_Filter.Substring(split(0), split(1))
Dim origfilter = SEARCH_Filter.Replace(temp_Filter, newsubstr)
origfilter = origfilter.Replace("SUBSTR[", "")
origfilter = origfilter.Replace("," & temp_area & "]", "")
SEARCH_Filter = origfilter
Else
If test = True Then
MsgBox("Die Length-Variablen für den Substring konnten nicht definiert werden", MsgBoxStyle.Exclamation)
Else
_Logger.Warn("AD GetAD_Attribut: Die Length-Variablen für den Substring konnten nicht definiert werden")
Exit Function
End If
End If
End If
Next
Next
End If
licenseManager = New ClassLicenseManager("#DigitalData9731258!#")
Dim PWDecode As String = licenseManager.DecodeLicenseKey(PW)
Dim objDirectoryEntry As DirectoryEntry = New DirectoryEntry(Domain)
If PW <> String.Empty And User <> String.Empty Then
objDirectoryEntry.Username = User
objDirectoryEntry.Password = PWDecode
End If
'Always use a secure connection
objDirectoryEntry.AuthenticationType = AuthenticationTypes.Secure
Dim searcher As New DirectorySearcher() 'rootEntry
searcher.SearchRoot = objDirectoryEntry 'New DirectoryEntry(Domain)
' get the LDAP filter string based on selections on the form
searcher.PropertyNamesOnly = True
searcher.PropertiesToLoad.Add(Property2Load)
'would also work and saves you some code
searcher.Filter = SEARCH_Filter
Dim results As SearchResultCollection
results = searcher.FindAll()
Dim result As SearchResult
Dim i As Integer = 0
Dim AttributValue As String
For Each result In results
ReDim Preserve ErgebnisAD(i)
AttributValue = result.GetDirectoryEntry.Properties.Item(Property2Load).Value
ErgebnisAD(i) = AttributValue
i += 1
Next
If i = 1 And AttributValue <> Nothing Then
Return AttributValue
Else
If i > 1 And AttributValue <> Nothing Then
Return ">1"
Else
If i = 1 And AttributValue = Nothing Then
Return ""
Else
Return "99"
End If
End If
End If
Catch ex As Exception
Return ex.Message
End Try
End Function
Public Shared Function LDAP_Return_Regex(stringWert As String)
' Regulären Ausdruck zum Auslesen der windream-Indexe definieren
Dim preg As String = "\[%{1}[a-zA-Z0-9\!\$\&\/\(\)\=\?\,\.\-\;\:_öÖüÜäÄ\#\'\+\*\~\{\}\@\€\<\>\ ]+]{1}"
' SQL-String für aktuelle Verknüpfung laden
Dim Zeichenfolge As String = stringWert
'Erstzen des Dateinamens - für CURSOR NachindexierungPr
' einen Regulären Ausdruck laden
Dim regulärerAusdruck As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(preg)
' die Vorkommen im SQL-String auslesen
Dim elemente As System.Text.RegularExpressions.MatchCollection = regulärerAusdruck.Matches(Zeichenfolge)
' alle Vorkommen der windream-Indexe im SQL-String durchlaufen
Dim i As Integer = 0
Dim result As String
For Each element As System.Text.RegularExpressions.Match In elemente
result = element.ToString
i += 1
Next
If i = 1 Then
Return result
Else
Return ">1"
End If
End Function
' form a filter string for the search in LDAP format
Public Shared Function FormFilter(objectClass As String, filter As String)
Dim result As String
result = String.Format("(&(objectClass={0})(sAMAccountName={1}))", objectClass, filter)
Return result
End Function
End Class