0

我尝试连接到 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
4

2 回答 2

1

此异常的完整详细信息是:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

通常,当您的代码中存在内存泄漏时会发生这种情况,但这也可能是由于执行该result = mySearcher.FindAll()行时搜索权限不足。

您尝试搜索的服务器,它们是否都支持通过 登录AuthenticationTypes.None?这会强制发生简单的绑定,某些目录实例可能不支持或不需要这种绑定。由于您正在定义DirectoryEntry'.Path属性,因此您可能想尝试一下AuthenticationTypes.ServerBind,因为它是为您定义DirectoryEntry.

另外,我强烈推荐购买 Softerra 的免费Ldap Browser 4.5的副本。

这将允许您尝试手动连接和搜索具有不同AuthenticationTypes定义的目录-在这个问题的上下文中,我认为定义不起作用的服务器与与您一起工作的服务器的不同之处同样重要当前代码。

于 2013-06-24T17:58:24.160 回答
0

我发现了我的错误。

我忘记在我的 web.config 中指定属性“attrName”,它找不到该属性并显示错误消息。

现在它可以工作了。

于 2013-06-26T08:37:33.050 回答