我有一个MVC应用程序需要登录并根据Active Directory验证用户。我正在使用PrincipalContext.ValidateCredentials
方法,但总是返回false
。
连接到服务器没有问题。问题似乎出现在ValidateCredentials
方法中。
这是我的代码:
public static bool IsAuthenticated(string domain, string username, string pwd) {
bool IsAuthenticated = false;
try {
PrincipalContext insPrincipalContext =
new PrincipalContext(ContextType.Domain, domain, "DC=c1w,DC=com");
username = "c1w\\" + username;
IsAuthenticated = insPrincipalContext.ValidateCredentials(username, pwd);
}
catch (Exception ex)
{
// Rethrow this exception
ExceptionPolicy.HandleException(ex, "Exception Policy");
}
return IsAuthenticated;
}
有人知道为什么会发生这种情况吗?
Domain\UserName
在 Negotiate 中不起作用。但是当在方法调用中显式提供ContextOptions
(如context.ValidateCredentials(userName, password, ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing)
)时,为什么它会起作用呢?这很令人困惑。 - managerger