2016-07-27 80 views
-1

我正在嘗試使用c#創建LDAP Cnnection。在.NET上創建LDAP連接

我發現這個服務器賦予LDAP服務器來測試

http://www.forumsys.com/en/tutorials/integration-how-to/ldap/online-ldap-test-server/

我用Google搜索許多後,試圖創建一個統一代碼

string domain = "ldap://ldap.forumsys.com/ou=mathematicians"; 
      string username = "cn=read-only-admin,dc=example,dc=com"; 
      string password = "password"; 
      string LdapPath = "Ldap://ldap.forumsys.com:389/ou=scientists,dc=example,dc=com"; 


      string domainAndUsername = domain + @"\" + username; 
      DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password); 
      try 
      { 
       // Bind to the native AdsObject to force authentication. 
       Object obj = entry.NativeObject; 
       DirectorySearcher search = new DirectorySearcher(entry); 
       search.Filter = "(SAMAccountName=" + username + ")"; 
       search.PropertiesToLoad.Add("cn"); 
       SearchResult result = search.FindOne(); 

       // Update the new path to the user in the directory 
       LdapPath = result.Path; 
       string _filterAttribute = (String)result.Properties["cn"][0]; 
      } 
      catch (Exception ex) 
      { 

       throw new Exception("Error authenticating user." + ex.Message); 
      } 

此代碼是不是連接它給意外的錯誤..

我也試過一些其他的證書,但他們也沒有幫助...

AUTH_LDAP_SERVER_URI = 「ldap://ldap.forumsys.com」 
AUTH_LDAP_BIND_DN = 「cn=read-only-admin,dc=example,dc=com」 
AUTH_LDAP_BIND_PASSWORD = 「password」 
AUTH_LDAP_USER_SEARCH = LDAPSearch(「ou=mathematicians,dc=example,dc=com」, 
ldap.SCOPE_SUBTREE, 「(uid=%(user)s)」) 

-------------------- 
$config[‘LDAP’][‘server’] = ‘ldap://ldap.forumsys.com'; 
$config[‘LDAP’][‘port’] = ‘389’; 
$config[‘LDAP’][‘user’] = ‘cn=read-only-admin,dc=example,dc=com'; 
$config[‘LDAP’][‘password’] = ‘password'; 

------------------------- 
$config[‘LDAP’][‘server’] = ‘ldap://ldap.forumsys.com/ou=mathematicians'; 
$config[‘LDAP’][‘port’] = ‘389’; 
$config[‘LDAP’][‘user’] = ‘gauss'; 
$config[‘LDAP’][‘password’] = ‘password'; 

-------------------------- 
OpenDSObject/GetObject functions, but don’t see a way to run a query with the ASDI objects. 
Set LDAP = GetObject(「LDAP:」) 
Set root = LDAP.OpenDSObject(「LDAP://ldap.forumsys.com:389″, 「cn=read-only-admin,dc=example,dc=com」, 「password」, 0) 
Set ou = LDAP.OpenDSObject(「LDAP://ldap.forumsys.com:389/ou=mathematicians,dc=example,dc=com」」, 「cn=read-only-admin,dc=example,dc=com」, 「password」, 0) 
Set user = LDAP.OpenDSObject(「LDAP://ldap.forumsys.com:389/uid=riemann,dc=example,dc=com」, 「cn=read-only-admin,dc=example,dc=com」, 「password」, 0) 

我需要一些建議我失蹤了。任何資源都將有所幫助

+0

**什麼**意外錯誤? –

+0

您是否嘗試過提供正確的協議:LDAP而不是Ldap或ldap?根據我剛剛搜索的http://forums.asp.net/t/1026497.aspx?LDAP+Problem+with+NET+Unknown+error+0x80005000+,這可能是問題的原因。 –

回答

0

嘗試使用PrincipalContext連接到LDAP服務器。這裏是一個很好的how-to文章中,我提到,當我入門:http://ianatkinson.net/computing/adcsharp.htm

ctx = new PrincipalContext(
    ContextType.Domain, 
    "contoso.local", 
    "OU=Security Groups,OU=Contoso Inc,DC=contoso,DC=local", 
    "contoso\sysadmin", 
    "[email protected]"); 
1

我有幾分相似的問題與此服務器和谷歌派我到這裏。

我看到的一個問題是LDAP路徑中區分大小寫的問題。我們也應該指定AuthenticationType。

請檢查以下代碼塊應該工作。

string ldapServer = "LDAP://ldap.forumsys.com:389/ou=scientists,dc=example,dc=com"; 
string userName = "cn=read-only-admin,dc=example,dc=com"; 
string password = "password"; 

var dirctoryEntry = new DirectoryEntry(ldapServer, userName, password, AuthenticationTypes.ServerBind); 

try { 
    object nativeObject = dirctoryEntry.NativeObject; 
    //Rest of the logic 
} catch (Exception ex) { 
    //Handle error 
}