如何在不考虑缓存域凭据的情况下验证域凭据

Vin*_*c 웃 20 c# windows security authentication

我想知道是否有办法验证域凭据并确保我们不使用Cached Domain Credential

我用它来验证凭证:

 bool valid = false;
 using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
 {
     valid = context.ValidateCredentials( username, password );
 }
Run Code Online (Sandbox Code Playgroud)

问题是当我更改密码时,旧密码仍然有效.

编辑:如果强制重置密码,则不会使用缓存的域凭据.但是在我们强制重置的那一刻,以及用户重置密码的那一刻,旧密码仍然有效.

JJS*_*JJS 5

问题已经有答案为什么Active Directory验证上次密码?

解决方案是使用Kerberos身份验证.

以下代码显示了如何仅使用Kerberos执行凭据验证.如果发生故障,正在使用的身份验证方法将不会回退到NTLM.

private const int ERROR_LOGON_FAILURE = 0x31;

private bool ValidateCredentials(string username, string password, string domain)
{
    NetworkCredential credentials
        = new NetworkCredential(username, password, domain);

    LdapDirectoryIdentifier id = new LdapDirectoryIdentifier(domain);

    using(LdapConnection connection = new LdapConnection(id, credentials, AuthType.Kerberos))
    {
        connection.SessionOptions.Sealing = true;
        connection.SessionOptions.Signing = true;

        try
        {
            connection.Bind();
        }
        catch (LdapException lEx)
        {
            if (ERROR_LOGON_FAILURE == lEx.ErrorCode)
            {
                return false;
            }

            throw;
        }

    return true;
}
Run Code Online (Sandbox Code Playgroud)