2

我试图让我的应用程序对存储在 OpenLDAP 上的用户进行身份验证。据我所知,没有适用于 .NET 的 API,并且只有适用于 Java 的库。

我尝试使用 DirectorySearcher 的 DirectoryEntry 没有成功,并且 LDAPConnection 也没有工作。

有没有人做过类似的事情?

4

2 回答 2

1

这对于 VB .Net 怎么样:

' for networkcredential
Imports System.Net
Imports System.DirectoryServices.Protocols.DirectoryConnection
Imports System.DirectoryServices.Protocols.LdapConnection
Imports System.DirectoryServices.Protocols.LdapDirectoryIdentifier

Public Function IsAuthenticated( ByVal username As String, ByVal pwd As String) As Boolean
          ' against OpenLDAP

        Dim strLDAPServer As String = String.Empty

        'users full DistinguishedName in OpenLDAP
        Dim uid As String = "UID=" & username & _
         ",ou=People,dc=example,dc=com"

        strLDAPServer = "my.openldapserver.com"

        Dim ldapDirectoryIdentifier As New System.DirectoryServices.Protocols.LdapDirectoryIdentifier(strLDAPServer, 389, True, False)
        Dim networkCredential As New NetworkCredential(uid, pwd)
        Try
            Dim ldap As New System.DirectoryServices.Protocols.LdapConnection(ldapDirectoryIdentifier, networkCredential)
            ldap.SessionOptions.SecureSocketLayer = False
            ldap.SessionOptions.ProtocolVersion = 3
            ldap.AuthType = ldap.AuthType.Basic
            ldap.Bind()

        Catch lex As Exception
            'Authentication fails - bad username or password

            Return False
        End Try



        Return True

    End Function

基于此处的 C# .Net 帖子:http: //blogs.msdn.com/b/alexch/archive/2012/05/07/sample-code-to-query-openldap-directory-via-net-system-directoryservices-协议.aspx

于 2014-02-04T16:32:12.270 回答
0
// Search for a user
DirectoryEntry entry = new DirectoryEntry(
                                          "LDAP://127.0.0.1/ou=People,dc=maxcrc,dc=com",
                                          "cn=Manager, dc=maxcrc, dc=com ",
                                          "secret",
                                          AuthenticationTypes.FastBind
                                         );
object obj = entry.NativeObject;

DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(cn=agent001)";
searcher.PropertiesToLoad.Add("cn");

SearchResult result = searcher.FindOne();
if (result != null)
    Console.WriteLine("Found");
else
    Console.WriteLine("Not found");
于 2013-05-20T09:06:10.100 回答