C# 使用ADAM用户和简单绑定的ADAM身份验证

C# 使用ADAM用户和简单绑定的ADAM身份验证,c#,authentication,active-directory,adam,C#,Authentication,Active Directory,Adam,我遵循了微软的ADAM分步指南,在本地机器上设置了一个ADAM实例。我正在尝试使用“Mary Baker”帐户进行身份验证,但每次我在下面的if(entry.Guid!=null)行上收到COM异常时。异常状态表示存在未知用户名或错误密码 但是,我可以使用ldp实用程序连接到ADAM并成功地执行简单的绑定-因此我知道用户名都存在,并且我有正确的密码 此外,我已将用户的msDS UserAccountDisabled属性设置为false,并将该用户添加到管理员和读者角色 有什么想法吗 pa

我遵循了微软的ADAM分步指南,在本地机器上设置了一个ADAM实例。我正在尝试使用“Mary Baker”帐户进行身份验证,但每次我在下面的
if(entry.Guid!=null)
行上收到COM异常时。异常状态表示存在未知用户名或错误密码

但是,我可以使用ldp实用程序连接到ADAM并成功地执行简单的绑定-因此我知道用户名都存在,并且我有正确的密码

此外,我已将用户的msDS UserAccountDisabled属性设置为false,并将该用户添加到管理员和读者角色

有什么想法吗

    path = "LDAP://localhost:50000/O=Microsoft,c=US";
    userId = "CN=Mary Baker,OU=ADAM users,";
    password = "Mary@101";

    DirectoryEntry entry = new DirectoryEntry(path, userId, password, AuthenticationTypes.None);
    if (entry.Guid != null)
        LoadWelcomeScreen();

谢谢。

我没有使用过ADAM或System.DirectoryServices,但我有使用LDAP和AD的经验;希望以下内容适用

我以前从未见过以这种格式给出的用户ID。(它看起来像是某种相对DN,如尾随的逗号所示?)您是否尝试将用户ID指定为完整DN(根据标准LDAP的要求)或裸用户名(如果ADAM支持)

当诊断像这样的网络协议问题时(看看我的程序是否在做我想让它做的事情,看看它所做的事情与一个正常运行的程序所做的事情相比如何),我发现运行非正常运行和正常运行的操作来查看它们之间的区别是很有帮助的。如果您从未使用过Wireshark,希望它不会太难开始:

  • 下载、安装并启动软件
  • 在“捕获”下,单击“选项”
  • 将接口设置为本地主机/环回或以太网接口。(我认为环回在Windows上并没有预期的效果;您可能希望选择以太网接口并在C代码中更新LDAP URL,以使用主机名而不是本地主机。)
  • 在捕获过滤器下,输入“tcp端口50000”(无引号)
  • 单击开始,运行连接操作,然后进入捕获菜单并单击停止

  • Wireshark可以为您分析协议,因此您自己不必太熟悉协议细节,尽管您知道的越多,解释所有细节就越容易。您可以启动Wireshark的几个实例来轻松比较两个不同的捕获(您的代码和LDP)。

    ADAM将用户的唯一标识符存储在
    用户
    类的
    displayName
    属性中。它们在ADAM实例中必须是唯一的,以便用户进行身份验证。如果两个用户都将其
    displayName
    属性设置为“jsmith”,则两个用户都无法在ADAM中进行身份验证


    使用ldp实用程序查询Mary Baker的
    displayName
    。它可能是类似“mbaker”的东西。将该值用作给定代码中的用户ID。

    感谢Ryan提供有关displayName的提示。将我的测试类发布在本地ADAM实例上,供可能感兴趣的人使用

        [TestMethod]
        public void CreateUserAccount()
        {
            var username = "amurray";
            var password = "ADAMComplexPassword1234";
            var firstname = "Andy";
            var lastname = "Murray";
    
            const AuthenticationTypes authTypes = AuthenticationTypes.Signing |
                                                  AuthenticationTypes.Sealing |
                                                  AuthenticationTypes.Secure;
    
            var ldapPath = "LDAP://localhost:389/OU=MyProject,OU=Applications,DC=Company,DC=ADAM";
            using (var dirEntry = new DirectoryEntry(ldapPath, "MyPC\\adamuser", "Password1!", authTypes))
            {
                DirectoryEntry user = null;
                const int ADS_PORT = 389;
                const long ADS_OPTION_PASSWORD_PORTNUMBER = 6;
                const long ADS_OPTION_PASSWORD_METHOD = 7;
                const int ADS_PASSWORD_ENCODE_CLEAR = 1;
    
                try
                {
                    user = dirEntry.Children.Add(string.Format("CN={0} {1}", firstname, lastname), "user");
                    user.Properties["displayName"].Value = username;
                    user.Properties["userPrincipalName"].Value = username;
                    user.Properties["msDS-UserAccountDisabled"].Value = false;
                    user.Properties["msDS-UserDontExpirePassword"].Value = true;
                    user.CommitChanges();
                    var userid = user.Guid.ToString();
    
                    // Set port number, method, and password.
                    user.Invoke("SetOption", new object[]{ADS_OPTION_PASSWORD_PORTNUMBER,ADS_PORT});
                    user.Invoke("SetOption", new object[]{ADS_OPTION_PASSWORD_METHOD,ADS_PASSWORD_ENCODE_CLEAR});
    
                    user.Invoke("SetPassword", new object[] {password});
                    user.CommitChanges();
                    user.Close();
                }
                catch (Exception e)
                {
                    var msg = e.GetBaseException().Message;
                    Console.WriteLine(e);
                    System.Diagnostics.Debug.Print(msg);
                }                
            }
        }
    
    
        [TestMethod]
        public void TestUserAuthentication()
        {
            try
            {
                var ldsContext = new PrincipalContext(ContextType.ApplicationDirectory, "localhost:389",
                                                      "OU=MyProject,OU=Applications,DC=Company,DC=ADAM",
                                                      ContextOptions.SimpleBind);
    
                // Returns true if login details are valid
                var isValid = ldsContext.ValidateCredentials("amurray", "ADAMComplexPassword1234", ContextOptions.SimpleBind);
            }
            catch (Exception e)
            {
                var msg = e.GetBaseException().Message;
                Console.WriteLine(e);
                System.Diagnostics.Debug.Print(msg);
            }
        }