我想检查 Windows 域上的用户/密码组合。现在我用下面的代码来做:
bool Login(String username, String password) {
var principalContext = new PrincipalContext(ContextType.Domain);
principalContext.ValidateCredentials(username, password);
}
虽然它有效,但让我烦恼的是我必须将密码放在
String
中。为了使用该 API;因为我正在使用 SecureString
要将密码存储在其他任何地方,我真的很想使用某种方法来检查用户名/密码组合,而不必将密码作为托管 System.String
传递。 .实现这一目标的最佳方法是什么?
最佳答案
您可以尝试的一种方法可能是:
通过使用 P/Invoke 调用 LoginUser 模拟用户,将密码作为 SecureString 传递 as described in MSDN .
使用模拟用户连接到 ActiveDirectory,无需传递用户名和密码:
AuthenticationTypes authenticationTypes = AuthenticationTypes.Secure;
using (var entry = new DirectoryEntry("LDAP://example.com", "", "", authenticationTypes))
{
...
}
我没有试过这个,但在我看来它应该有效。
关于.net - 如何在不将密码放入字符串的情况下检查 ActiveDirectory 上的用户/密码组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5548235/