1

我已经设置了一个 ADAM 实例并添加了一些测试用户。在 c# 中,我可以使用 Windows 帐户绑定到 ADAM,但无法使用 ADAM 用户之一进行绑定。(我可以在 ldp 中成功绑定 adam 用户)并且我已通过将 msDS-UserAccountDisabled 属性设置为 false 来确保启用了用户。当我与我的 Windows 帐户绑定时,我可以成功搜索并带回 ADAM 用户的属性,但我仍在努力对他们进行身份验证,当我尝试与 ADAM 用户帐户绑定时,我收到错误:

错误:System.Runtime.InteropServices.COMException (0x8007052E):登录失败:未知用户名或密码错误。在 System.DirectoryServices.DirectoryEntry.Bind(布尔 throwIfFail)

这是我正在使用的代码:

string userName = txtUserName.Text;
string password = txtPassword.Text;
string ADConnectionString = "LDAP://localhost:389/CN=sandbox,DC=ITOrg";
DirectoryEntry entry = new DirectoryEntry(ADConnectionString);

entry.Username = "myComputer\\Administrator";
entry.Password = "myPassword";
try 
{
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=user)(CN=" + userName + "))";
SearchResultCollection result = searcher.FindAll();
if (result.Count > 0)
{
    //bind with simple bind
    using (DirectoryEntry de = new DirectoryEntry(result[0].Path, userName, password,AuthenticationTypes.None))
    {
         if (de.Guid != null) // this is the line where it dies
         {
              Label1.Text = "Successfully authenticated";
              Label2.Text = result[0].Properties["displayName"][0].ToString();
              Label3.Text = result[0].Properties["telephoneNumber"][0].ToString();
          } else 
          {
             Lable1.Text = "Unable to Authenticate";
          }
     }
}
else
{
    Lable1.Text = "UserName :" + userName + " not found"; 
}
} catch(Exception ex)
{
     Label1.Text = "Error searching: " + ex.ToString();
}

在此先感谢您的帮助,非常感谢!

4

1 回答 1

6

这可能是用户名格式问题。在 SDS 中验证 ADAM 用户时,您必须使用 LDAP 简单绑定并使用 ADAM 支持的名称格式。ADAM 在技术上也允许您使用 Digest auth,但这在 SDS(仅 SDS.Protocols)中不可用,因此不适用于您的代码方法。

您正在使用简单绑定,因为您设置了 AuthenticationTypes.None 以便该部分正常。那么可能错误的部分是用户名格式。

ADAM 接受用户的完整 DN、他们的 displayName(如果设置且唯一)和/或 userPrincipalName(如果设置且唯一)作为“可绑定”用户名,因此从用户的完整 DN 开始,看看是否有效。如果是这样,您也可以尝试其他用户名值。请注意,您可以在 ADAM 中为 displayName 或 userPrincipalName 放置您想要的任何内容。没有验证。只要确保值是唯一的。

如果您真的想针对 ADAM 执行某种类型的绑定身份验证,您将通过使用 .NET 3.5 中 PrincipalContext 的 ValidateCredentials 方法获得更好的性能和扩展性。

这种东西一直在http://www.directoryprogramming.net的论坛中记录和讨论,并且是我经常光顾的地方,因为它是我的网站。:) 一位朋友向我介绍了这篇文章,否则我将永远不会看到它。

于 2009-05-14T21:52:06.563 回答