活动目录身份验证

问题描述:

我正在使用下面的代码在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; 
+0

这是一个很好的问题。出于好奇,你使用了什么AuthenticationType? – Pandincus 2011-02-12 07:19:33

+0

你是什么意思的AuthenticationType,我使用System.DirectoryServices;名称空间和提到的身份验证代码 – 2011-02-12 07:51:00

如果您使用的是.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 constructorDirectoryEntry的:

DirectoryEntry entry = 
    new DirectoryEntry(path, username, pwd, 
         AuthenticationTypes.SecureSocketsLayer); 

有没有其他办法可以做到这一点,我害怕 - 我不认为有什么办法可以让你在客户端以与Windwos Server/Active Directory相同的方式散列密码,并传入散列值...