【问题标题】:Could not validate a username and password无法验证用户名和密码
【发布时间】:2016-04-20 21:36:26
【问题描述】:

我需要验证用户输入对当前会话窗口是否正确。因此我使用下面的代码:

private void LoginUser(string username, string password)
{
    bool isCredentialValid = false;

    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
    {
        isCredentialValid = context.ValidateCredentials(username, password);
    }
    if (isCredentialValid)
    {
        //
    }
    else
    {
        //
    }
}

我总是得到错误ValidateCredentials结果的问题。

Rq:我正在使用 .Net 4.5 框架

【问题讨论】:

  • 这是一个 .Net 方法
  • 确保应用程序可以使用正确的凭据访问域
  • 检查您的用户名或密码是否有多余的字符
  • @irvgk 他们是正确的,我需要指定域

标签: c# .net


【解决方案1】:

来自 MSDN http://msdn.microsoft.com/en-us/library/bb154889.aspx

ValidateCredentials 方法绑定到在 构造函数。如果用户名和密码参数为空,则 验证构造函数中指定的凭据。如果不 凭证在构造函数中指定,用户名和 密码参数为空,此方法验证默认值 当前主体的凭据。

在 PrincipalContext 构造函数中,您也可以指定要检查的凭据。

 using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, 
                   container, ContextOptions.SimpleBind, username, password))
{
    return pc.ValidateCredentials(domain + @"\" + username, password,
                               ContextOptions.SimpleBind);
}

试试这个。

【讨论】:

  • 不要使用ContextOptions.SimpleBind,它会以明文形式发送您的密码
  • @irvgk,No, ContextOptions.SimpleBind 表示使用 Basic Authentication 。这并不意味着发送明文密码。
  • 感谢您的回答,即使不指定 ContextOptions 也可以使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 2013-12-26
  • 2016-01-24
  • 2019-06-27
  • 1970-01-01
相关资源
最近更新 更多