使用PHP连接到LDAP成功但无法连接到C#
问题描述:
我使用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);
}
谢谢!
答
它可能是你的库没有实现LDAP,而是一个叫做ActiveDirectory的奇怪的非标准Microsoft版本的LDAP,它只在服务器是实际的ActiveDirectory服务器时才起作用,并且在你使用时不太容易工作非微软服务器,如OpenLDAP?
难道是?
你可以在AD中使用'DirectoryEntry',但对于通用的LDAP请求,你应该看一下'System.DirectoryServices.Protocols'。还发现[本博客](https://auth0.com/blog/using-ldap-with-c-sharp/)。 – EricLavault
感谢EricLavault,这个博客似乎真的很有帮助,我会尝试一下。 –