我尝试连接到 LDAP 服务器以读取联系人并将它们保存到我的表中。
在某些服务器上,我的代码可以工作,但在许多其他服务器上却不行。我收到此错误:尝试读取或写入受保护的内存错误
我不知道问题是什么。
这是我的代码,希望你能帮助我:
Protected Sub ADLoader()
'Holt die Verbindungdaten aus dem web.config
config = DirectCast(ConfigurationManager.GetSection("ldapSettings"), NameValueCollection)
Dim server, username, password, attrsAMAccountName, basisDN, filterAttr, attrNachname, attrVorname, attrEmail, attrTel, attrAbteilung, attrPlz, attrStadt, attrStrasse, attrTrigram, attrName As String
Dim i As Integer = 0
server = config.[Get]("server")
username = config.[Get]("username")
password = config.[Get]("password")
basisDN = config.[Get]("basisDN")
filterAttr = config.[Get]("filterAttr")
attrNachname = config.[Get]("attrNachname")
attrVorname = config.[Get]("attrVorname")
attrEmail = config.[Get]("attrEmail")
attrTel = config.[Get]("attrTel")
attrAbteilung = config.[Get]("attrAbteilung")
attrPlz = config.[Get]("attrPlz")
attrStadt = config.[Get]("attrStadt")
attrStrasse = config.[Get]("attrStrasse")
attrTrigram = config.[Get]("attrTrigram")
attrName = config.[Get]("attrName")
attrsAMAccountName = config.[Get]("attrsAMAccountName")
If CustomUser = "FIFA" Then
attrsAMAccountName = config.[Get]("attrTrigram")
End If
Dim pfad As String = "LDAP://" + server + "/" + basisDN
Dim filter As String = " (&(objectCategory=user)(" + filterAttr + "=*))"
Dim results As SearchResultCollection = Nothing
'Baut die Verbindung zum Active Directory auf und speichert die Daten in eine Liste
Try
Dim entry As New DirectoryEntry
entry.Path = pfad
entry.Username = username
entry.Password = password
entry.AuthenticationType = AuthenticationTypes.None
Dim mySearcher As New DirectorySearcher(entry)
mySearcher.PropertiesToLoad.Add(attrNachname)
mySearcher.PropertiesToLoad.Add(attrVorname)
mySearcher.PropertiesToLoad.Add(attrEmail)
mySearcher.PropertiesToLoad.Add(attrTel)
mySearcher.PropertiesToLoad.Add(attrAbteilung)
mySearcher.PropertiesToLoad.Add(attrPlz)
mySearcher.PropertiesToLoad.Add(attrStadt)
mySearcher.PropertiesToLoad.Add(attrName)
mySearcher.PropertiesToLoad.Add(attrsAMAccountName)
'mySearcher.PropertiesToLoad.Add(zusatzFeld)
mySearcher.Filter = filter ' Sucht nur in der Kategorie user und nur mit trigram
mySearcher.Sort.PropertyName = attrNachname
mySearcher.PageSize = 100
Dim result As SearchResultCollection
result = mySearcher.FindAll() <<<<<<<<<<< HERE IS THE ERROR >>>>>>>
log.Debug("AD Importer: Gefundene Personen im Active Directory = " + result.Count.ToString)
Dim searchResult As SearchResult
For Each searchResult In result
Dim propcoll As ResultPropertyCollection = searchResult.Properties
Dim ssoBenutzer As New BiObPerson
For Each key As String In propcoll.PropertyNames
If key = attrsAMAccountName Then
For Each values As Object In propcoll(key)
ssoBenutzer.BenutzerName = values.ToString()
ssoBenutzer.UniqueName = ssoBenutzer.BenutzerName
Next
End If
If key = attrAbteilung Then
For Each values As Object In propcoll(key)
ssoBenutzer.Abteilung = values.ToString()
Next
End If
If key = attrNachname Then
For Each values As Object In propcoll(key)
ssoBenutzer.Nn = values.ToString()
Next
End If
If key = attrVorname Then
For Each values As Object In propcoll(key)
ssoBenutzer.Vn = values.ToString()
Next
End If
If key = attrEmail Then
For Each values As Object In propcoll(key)
ssoBenutzer.Email = values.ToString()
Next
End If
If key = attrTel Then
For Each values As Object In propcoll(key)
ssoBenutzer.Tel = values.ToString()
Next
End If
If key = attrStrasse Then
For Each values As Object In propcoll(key)
ssoBenutzer.Streetaddress = values.ToString()
Next
End If
If key = attrStadt Then
For Each values As Object In propcoll(key)
ssoBenutzer.StadtName = values.ToString()
Next
End If
Next
i = 1 + i
log.Debug("AD Importer: Folgende Person wird in die Datenbank hinzugefügt = " + ssoBenutzer.BenutzerName)
CreateOrUpdateSSOUser(ssoBenutzer, username)
Next
Label1.Text = "(" & i & ")" & " Fertig. "
Catch ex As Exception
Debug.WriteLine("AD Importer: Fehlgeschlagen=" + ex.Message)
log.Debug("AD Importer: Fehler bei der Verbindung zum AD Server" + ex.Message)
End Try
End Sub