【问题标题】:C# LDAP Authentication works for one DC, but not anotherC# LDAP 身份验证适用于一个 DC,但不适用于另一个
【发布时间】:2013-02-01 00:08:20
【问题描述】:

我有一个有趣的问题我已经尝试解决了几天。

我目前正在使用运行 Active Directory 标准实例的 Windows Server 2003 机器。

该目录包含两个域组件 (DC),它们都包含将通过我的应用程序对目录进行授权的用户。

我正在使用:

  • 服务器的 IP 地址作为主机名
  • 通过端口 3269 的 SSL 连接
  • GSS 协商身份验证机制
  • 作为两个 DC 的 parentDN 的 BaseDN
  • sAMAccountName 作为登录名

问题是,我无法成功授权来自 DC1 的任何用户,但属于 DC2 的所有用户都完全没问题并且工作得很好。我在 DC1 上收到此错误:

8009030C: LdapErr: DSID-0C09043E, comment: AcceptSecurityContext error, data 0, vece System.DirectoryServices.Protocols.LdapException: The supplied credential is invalid.

但是,使用 Softerra 的 LDAP Broswer,我可以毫无问题地连接并授权相同的用户,因此我知道凭据是正确的。

据我所知,这两个 DC 的配置相同...我已经浏览了它们两个以寻找不同的东西...但没有发现任何真正突出的东西。

几个月前我发布了一些关于这个特定设置的内容,我正在使用的代码也在那个线程中。

Set callback for System.DirectoryServices.DirectoryEntry to handle self-signed SSL certificate?

我们将不胜感激。

谢谢!

【问题讨论】:

    标签: c#-4.0 active-directory


    【解决方案1】:

    我能够让这个工作,但对于我的生活,我无法弄清楚为什么会这样。基本上就是这个错误……

    8009030C: LdapErr: DSID-0C09043E, comment: AcceptSecurityContext error, data 0, vece    System.DirectoryServices.Protocols.LdapException: The supplied credential is invalid.
    

    ...死了。问题是在我称之为 DC2 的用户下登录需要使用域和 sAMAccountName(例如 LIB\JSmith)发出绑定,而不是 DC1,它只允许输入 sAMAccountName。

    我认为使这个程序化的最佳方法是使用主要绑定帐户来查询用户的 DN。从那个 DN,使用一些狡猾的 RegEx,我能够捕获他们继承的域,并发出两个单独的绑定。

    SearchResultEntry ResultEntry = userResponse.Entries[0];
    
    //Let's get the root domain of the user now using our DN RegEx and that search result
    Regex RegexForBaseDN = new Regex(config.LdapAuth.LdapDnRegex);
    Match match = RegexForBaseDN.Match(ResultEntry.DistinguishedName);
    string domain = match.Groups[1].Value;
    
    //Try binding the user with their domain\username
    try
    {
        var thisUser = new NetworkCredential{
                                             Domain = domain, 
                                             UserName = username, 
                                             Password = Pin
                                            };
    
         //If this goes well, we'll continue forward
         ldapconn.Bind(thisUser);
    }
    
    //If that doesn't work, try biding them with the highest level domain
    catch (LdapException ex)
    {
         if (ex.ErrorCode.Equals(LdapErrorCodes.LDAP_INVALID_CREDENTIALS))
         {
              var thisUserOnce = new NetworkCredential{
                                                       Domain = config.LdapAuth.LdapDomain,
                                                       UserName = username,
                                                       Password = Pin
                                                      };
    
              //If this goes well, we'll continue forward
              ldapconn.Bind(thisUserOnce);
          }
     }
    

    它没有我想要的那么优雅,但它确实适用于这种特殊情况。

    但是,我仍然对为什么命名约定因用户继承的 DC 不同而有所不同非常感兴趣。

    【讨论】:

      猜你喜欢
      • 2016-07-03
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 2020-09-08
      • 2016-11-11
      • 2014-08-16
      • 2017-10-29
      相关资源
      最近更新 更多