2017-11-17 154 views
0

我使用PHP成功連接到LDAP,嘗試了很多東西,但是當我嘗試使用C#時,總是會收到「服務器不可操作」或「 LDAP服務器不可用「。 這裏是PHP代碼:使用PHP連接到LDAP成功但無法連接到C#

<?php 
function login($username='user', $password='pass') { 
     if (empty($username) || empty($password)) { 
      throw new BadCredentialsException('Invalid username or password.'); 
     } 
     if (($ldap = @ldap_connect($url = 'ldap://ds.xyz-example.com', $port = 636)) === false) { 
      echo('Error connecting LDAP server with url %s on port %d.'); 

     } 
     if ([email protected]_bind($ldap, sprintf($dn='uid=%s,ou=People,dc=xyz-example,dc=com', $username), $password)) { 
      $error = ldap_errno($ldap); 
      if ($error === 0x31) { 
       echo('Invalid username or password.'); 

      } else { 
       echo('error during authentication with LDAP.'); 
      } 
     } 
     return true; 
    } 


login(); // call the function 
?> 

這是工作完美,但我需要用C#。我怎樣才能用C#使用端口和dn以及用戶並傳遞?

這是我試圖用C#但有錯誤「服務器是不可操作」提前

string ldapPath = "LDAP://ds.xyz-example.com:636/UID=user,OU=People,DC=xyz-example,DC=com"; 
      string user = "user"; 
      string password = "pass"; 

      DirectoryEntry deSSL = new DirectoryEntry(ldapPath, user, password, AuthenticationTypes.SecureSocketsLayer); 
      try 
      { 
       user = deSSL.Properties["uid"][0].ToString(); //if this works, we bound successfully 
       Response.Output.Write("Success... {0} has bound", user); 
      } 
      catch (Exception ex) 
      { 
       Response.Output.Write("Bind Failure: {0}", ex.Message); 
      } 

謝謝!

+0

你可以在AD中使用'DirectoryEntry',但對於通用的LDAP請求,你應該看一下'System.DirectoryServices.Protocols'。還發現[本博客](https://auth0.com/blog/using-ldap-with-c-sharp/)。 – EricLavault

+0

感謝EricLavault,這個博客似乎真的很有幫助,我會嘗試一下。 –

回答

0

它可能是你的庫沒有實現LDAP,而是一個叫做ActiveDirectory的奇怪的非標準Microsoft版本的LDAP,它只在服務器是實際的ActiveDirectory服務器時才起作用,並且在你使用時不太容易工作非微軟服務器,如OpenLDAP?

難道是?