我尝试连接到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
答案 0 :(得分:1)
此例外的完整细节是:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
通常在您的代码中存在内存泄漏时会发生这种情况,但这也可能是由于执行result = mySearcher.FindAll()
行时搜索权限不足。
您尝试搜索的服务器是否都通过AuthenticationTypes.None
支持登录?这会强制发生简单绑定,某些目录实例可能不支持或不需要这种绑定。由于您要定义DirectoryEntry
的{{1}}属性,因此您可能需要尝试使用.Path
,因为它是为您定义AuthenticationTypes.ServerBind
的方式而构建的。 / p>
另外,我高度建议购买一份Softerra的免费Ldap Browser 4.5。
这将允许您尝试手动连接并搜索定义了不同DirectoryEntry
的目录 - 在此问题的上下文中,我认为定义不起作用的服务器的不同之处同样重要,与使用当前代码的那些相比。
答案 1 :(得分:0)
我发现了我的错误。
我忘了在web.config中指定属性“attrName”,它找不到属性并显示错误消息。
现在可行。