针对 Active Directory 验证用户名和密码? [英] Validate a username and password against Active Directory?
问题描述
如何根据 Active Directory 验证用户名和密码?我只想检查用户名和密码是否正确.
How can I validate a username and password against Active Directory? I simply want to check if a username and password are correct.
推荐答案
如果您使用 .NET 3.5 或更高版本,您可以使用 System.DirectoryServices.AccountManagement
命名空间并轻松验证您的凭据:
If you work on .NET 3.5 or newer, you can use the System.DirectoryServices.AccountManagement
namespace and easily verify your credentials:
// create a "principal context" - e.g. your domain (could be machine, too)
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
// validate the credentials
bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}
它很简单,很可靠,它是 100% 的 C# 托管代码——你还能要求什么?:-)
It's simple, it's reliable, it's 100% C# managed code on your end - what more can you ask for? :-)
在此处阅读所有相关信息:
Read all about it here:
更新:
如其他 SO 问题(及其答案)中所述,此调用存在一个问题,可能为用户的旧密码返回 True
.请注意这种行为,如果发生这种情况不要太惊讶:-)(感谢@MikeGledhill 指出这一点!)
As outlined in this other SO question (and its answers), there is an issue with this call possibly returning True
for old passwords of a user. Just be aware of this behavior and don't be too surprised if this happens :-) (thanks to @MikeGledhill for pointing this out!)
这篇关于针对 Active Directory 验证用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!