【问题标题】:Active directory authentication活动目录身份验证
【发布时间】:2011-02-12 06:15:33
【问题描述】:

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

【问题讨论】:

  • 这是个好问题。出于好奇,您使用的是什么 AuthenticationType?
  • 你的AuthenticationType是什么意思,我用的是System.DirectoryServices;用于身份验证的名称空间和提到的代码

标签: asp.net active-directory


【解决方案1】:

如果您使用的是 .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,则需要查看您可以在 DirectoryEntryfourth overloaded constructor 中定义的 AuthenticationTypes

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

恐怕没有其他方法可以做到这一点 - 我认为您在客户端没有任何方法可以像 Windwos Server / Active Directory 那样散列密码并传递哈希值...

【讨论】:

  • 仍然失败:(。也许我有问题。所以,如果我有 LDAP 服务器https://example.com,我应该将什么作为path 参数传递给DirectoryEntry 构造函数?谢谢
  • @BornToCode:您需要找到服务器的正确 LDAP 路径 - 类似于 LDAP://YourServer/DC=YourCOmpany,dc=com - https 链接并没有太大帮助...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
相关资源
最近更新 更多