我编写了以下代码来连接LDAP服务器并验证用户凭据。
public static string AuthFunction_One(string identity, string password, string containerString, string adServerName, bool useLDAPS, IdentityType identityType)
{
string failedString = "FAILED";
string successString = "SUCCESS";
string returnValue = failedString;
try
{
PrincipalContext ctx = null;
ctx = new PrincipalContext(ContextType.Domain, "ldap://localhost:10389/dc=example,dc=com", "uid=rish,dc=example,dc=com");
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, identityType, identity);
PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
if (ctx.ValidateCredentials(identity, password))
{
return successString;
}
else
{
return failedString;
}
}
catch (Exception ex)
{
NLogHelper.GetInstance().Log("ADUtilityClass", "AuthFunction_One", NLog.LogLevel.Debug, "Error in function. Ex: " + ex.ToString());
return failedString;
}
}
这是抛出以下异常。
Exception: Exception thrown:
'System.DirectoryServices.AccountManagement.PrincipalServerDownException' in System.DirectoryServices.AccountManagement.dll ("The server could not be contacted.")
当我尝试通过LDAP资源管理器进行连接时,它确实是connect.Below是我在那里使用的配置。
userdn -> uid=rish,dc=example,dc=com
basedn -> dc=example,dc=com
password -> secret
servername -> localhost
port -> 10389
version -> 3
我在C#代码中做错了什么?任何帮助将不胜感激。