c# - 从 C# 针对 ADAM 验证 ADAM 用户 - 无法绑定(bind)

标签 c# authentication directoryservices adam

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

错误: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();
}

预先感谢您的帮助,非常感谢!

最佳答案

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

您正在使用简单绑定(bind),因为您设置了 AuthenticationTypes.None,因此该部分没问题。可能错误的部分是用户名格式。

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

如果您确实想对 ADAM 进行某种类型的绑定(bind)身份验证,则可以通过使用 .NET 3.5 中的 PrimaryContext 的 ValidateCredentials 方法获得更好的性能和扩展性。

这类内容在 http://www.directoryprogramming.net 的论坛中进行了记录和讨论。一直都是我经常光顾的地方,因为它是我的网站。 :) 一位 friend 向我推荐了这篇文章,否则我永远不会看到它。

关于c# - 从 C# 针对 ADAM 验证 ADAM 用户 - 无法绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/864015/

相关文章:

c# - 如何在 Active Directory 中查询名称中包含美元符号 ($) 的用户?

c# - 自动登录 Active Directory

python - session 不可用,因为未设置 key 。将应用程序上的 secret_key 设置为唯一且 secret 的内容。 flask /英雄

c# - Active Directory 组在创建后无法立即可用

.net - System.DirectoryServices 中的间歇性异常

c# - 如何使用 Roslyn 声明 var 变量?

c# - 比较和交换作为非原子操作

c# - 从数据库获取值时 DBNULL.Value 始终返回 True

java - 在 Android 上设置谷歌登录时出现 "FirebaseError: Invalid authentication credentials provided"错误

python - 如何在python中将用户名和密码传递给cassandra