我想知道是否有办法validation域凭证,并确保我们不使用caching域凭证 ?
我用这个来validation证书:
bool valid = false; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { valid = context.ValidateCredentials( username, password ); }
问题是当我更改密码时,旧密码仍然有效。
编辑 :如果您强制密码重置, caching的域凭据将不会被使用。 但是在我们强制重置的那一刻,以及用户重置密码的那一刻之间,旧的密码仍然会工作。
问题已经有答案为什么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; }
你可以尝试这样的事情
try { using (var directoryEntry = new DirectoryEntry(ldapPath, userName, password)) { var invocation = directoryEntry.NativeObject; return true; } } catch (Exception ex) { return false; }