2012-11-12 48 views
2

我試圖讓我的應用程序來驗證存儲在OpenLDAP上的用戶。據我所知,沒有.NET的API,只有Java可用的庫。NET與OpenLDAP

我試過DirectorySetry與DirectorySearcher沒有成功,LDAPConnection也沒有工作。

有沒有人在類似的工作?

+2

如果指定了語言標記它會幫助,讓更多的人可以看到你的問題,並希望提供適當的答案。 –

+0

我以爲.net就夠了。我的目標是獲得更具體的答案,而不是粘貼他們在Google上找到的內容的人...... :) – Ted

+2

是的,但我必須添加.NET標記:) –

回答

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"); 
+0

您可能想在回答問題時提供更多信息。 – BenC

+0

哪裏是「認證」位? :) – Ted

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/alextch/archive/2012/05/07/sample-code-to-query-openldap-directory-via-net-system-directoryservices-protocols.aspx

+0

謝謝,我會試一試,讓你知道。 – Ted