从C#对ADAM用户进行身份验证 - 无法绑定

时间:2009-05-14 15:24:51

标签: c# authentication directoryservices adam

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

错误:System.Runtime.InteropServices.COMException(0x8007052E):登录失败:未知的用户名或密码错误。在System.DirectoryServices.DirectoryEntry.Bind(Boolean 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();
}

提前感谢您的帮助,非常感谢!

1 个答案:

答案 0 :(得分:6)

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

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

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

如果你真的想对ADAM做一些类型的绑定认证,你可以通过使用.NET 3.5中的PrincipalContext的ValidateCredentials方法获得更好的性能和扩展。

http://www.directoryprogramming.net的论坛上记录并讨论了这类内容,这是我经常访问的地方,因为它是我的网站。 :)一位朋友给我发了这篇文章或者我从未见过它。