fix finduserwithfilter

This commit is contained in:
Jonathan Jenne 2019-11-12 11:16:02 +01:00
parent a2b0959f22
commit d9df329256

View File

@ -168,10 +168,13 @@ Public Class ActiveDirectoryInterface
Dim oUserEx As UserPrincipalEx = UserPrincipalEx.FindByIdentity(oContext, IdentityType.SamAccountName, oUser.SamAccountName)
Dim oCustomAttributes As New List(Of ADUser.CustomAttribute)
If FindUserWithFilter(oUserEx, Filter) = False Then
Dim oUserFound = FindUserWithFilter(oUserEx, Filter)
If oUserFound = False Then
_logger.Debug("User '{0}' was skipped out due to user filter.", oUserEx.SamAccountName)
Continue For
End If
_logger.Debug("User '{0}' passed the filter.", oUserEx.SamAccountName)
' TODO: Figure out why oUserEx can be nothing for certain users
If oUserEx IsNot Nothing Then
@ -225,33 +228,40 @@ Public Class ActiveDirectoryInterface
End Function
Public Function FindUserWithFilter(User As UserPrincipalEx, Filter As String) As Boolean
Dim oRootPath = String.Join(","c, User.DistinguishedName.Split(","c).Skip(1))
Dim oPlaceholder = "@SAMACCOUNTNAME"
Dim oEntry As New DirectoryEntry("LDAP://" & oRootPath) With {
.Username = Nothing,
.Password = Nothing,
.AuthenticationType = AuthenticationTypes.Secure
}
Try
Dim oRootPath = String.Join(","c, User.DistinguishedName.Split(","c).Skip(1))
Dim oPlaceholder = "@SAMACCOUNTNAME"
Dim oProtocol = "LDAP://"
Dim oEntry As New DirectoryEntry(oProtocol & oRootPath) With {
.Username = Nothing,
.Password = Nothing,
.AuthenticationType = AuthenticationTypes.Secure
}
If Filter = String.Empty Then
_logger.Warn("FindUserWithFilter: Filter was empty, returning True for User {0}", User.SamAccountName)
Return True
End If
If Filter = String.Empty Then
_logger.Warn("FindUserWithFilter: Filter was empty, returning True for User {0}", User.SamAccountName)
Return True
End If
If Filter.Contains(oPlaceholder) Then
Filter = Filter.Replace(oPlaceholder, User.SamAccountName)
Else
_logger.Warn("FindUserWithFilter: Placeholder '{0}' was not found in filter. Results may not be correct.")
End If
If Filter.Contains(oPlaceholder) Then
Filter = Filter.Replace(oPlaceholder, User.SamAccountName)
Else
_logger.Warn("FindUserWithFilter: Placeholder '{0}' was not found in filter. Results may not be correct.")
End If
Dim oSearcher As New DirectorySearcher(oEntry, Filter)
Dim oResult = oSearcher.FindOne()
Dim oSearcher As New DirectorySearcher(oEntry, Filter)
Dim oResult As SearchResult = oSearcher.FindOne()
If oResult Is Nothing Then
If oResult IsNot Nothing AndAlso oResult.Path.Replace(oProtocol, String.Empty) = User.DistinguishedName Then
Return True
Else
Return False
End If
Catch ex As Exception
_logger.Warn("FindUserWithFilter: Unhandled exception.")
_logger.Error(ex)
Return False
Else
Return True
End If
End Try
End Function
Private Function GetRootNode() As DirectoryEntry