我正在尝试让我的应用程序对存储在OpenLDAP上的用户进行身份验证。据我所知,没有适用于.NET的API,只有Java可用的库。
我已经使用DirectorySearcher尝试使用DirectoryEntry但没有成功,并且LDAPConnection也无法正常工作。
有没有人做过类似的事情?
答案 0 :(得分: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
答案 1 :(得分: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");