我使用下面的代码来验证 Active Directory 中的用户,但密码以明文形式发送。如何散列我的密码,然后将其发送到 Active Directory?
DirectoryEntry entry = new DirectoryEntry(path, username, pwd);
try
{
//Bind to the native AdsObject to force authentication.
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
最佳答案
如果您使用 .NET 3.5,那么我强烈建议您改用 System.DirectoryServices.AccountManagement
命名空间(阅读所有相关信息: Managing Directory Security Principals in the .NET Framework 3.5 )。
在 S.DS.AM
中,很多事情都变得容易多了- 像验证用户:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
ctx.ValidateCredentials("test", "test", ContextOptions.SecureSocketLayer);
安全地执行此操作的唯一方法是指定
ContextOptions.SecureSocketLayer
强制使用 SSL 保护连接的选项。如果您无法迁移到 .NET 3.5 和
S.DS.AM
,您需要查看 AuthenticationTypes
您可以在 fourth overloaded constructor 中定义的 DirectoryEntry
:DirectoryEntry entry =
new DirectoryEntry(path, username, pwd,
AuthenticationTypes.SecureSocketsLayer);
恐怕没有其他方法可以做到这一点 - 我认为客户端没有任何方法可以像 Windwos Server/Active Directory 那样散列密码,并传入散列值。 ..
关于asp.net - 事件目录认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4976662/