OpenLdap C#使用可分辨名称中的转义字符绑定

本文关键字:转义字符 绑定 OpenLdap | 更新日期: 2023-09-27 18:00:03

我有一些可用的LDAP代码,我们在其中重新绑定到找到的用户,以便使用其可分辨名称验证用户。实际上,这就是正在发生的事情:

            string userDn = @"cn=Feat Studentl+umanroleid=302432,ou=Faculty of Engineering & Physical Sciences Administration,ou=Faculty of Engineering & Physical Sciences,ou=People,o=University of TestSite,c=GB";
            string fullPath = @"LDAP://surinam.testsite.ac.uk:636/" + userDn;
            DirectoryEntry authUser = new DirectoryEntry(fullPath, userDn, "mypassword", AuthenticationTypes.None);
            authUser.RefreshCache();

但是,这会在DirectoryEntry中导致未知错误80005000。绑定()

我怀疑问题可能是DN的CN属性中有一个"+"和一个"="。因此,在发现逃离这种情况的方法应该是使用''和字符的十六进制值后,我尝试了这个:

            string userDn = @"cn=Feat Studentl'2Bumanroleid'3D302432,ou=Faculty of Engineering & Physical Sciences Administration,ou=Faculty of Engineering & Physical Sciences,ou=People,o=University of TestSite,c=GB";

然而,我得到了错误:

登录失败:未知用户名或密码错误

我想这是因为现在它对请求很满意,但由于某种原因,它无法匹配用户DN。

这附近有什么吗?

OpenLdap C#使用可分辨名称中的转义字符绑定

根据我开发LDAP服务的经验,无论何时由于凭据无效而导致登录失败,都会成为绑定尝试的问题。您收到该错误是因为DirectoryEntry没有分析DN中的转义字符。。。然而,你一开始就不应该这么做。

在您的代码中,将AuthenticationTypes设置为"None"会强制条目根据您提供的DN进行简单绑定。由于您将服务器名称作为路径的一部分,我会尝试使用ServerBind auth类型,如下所示:

string LdapPath = ("LDAP://" + ldapUrl + "/" + Domain);
//Build the user and issue the Refresh bind
var dirEntry = new DirectoryEntry
                   {
                       Path = LdapPath,
                       Username = _usernameToVerify,
                       Password = _passwordToVerify,
                       AuthenticationType = AuthenticationTypes.ServerBind
                   };
//This will load any available properties for the user
dirEntry.RefreshCache();

此外,看起来您正在对安全LDAP端口(636)进行此调用,因此请确保还包括AuthenticationTypes。SecureSocketsLayer与ServerBind mechansim:

AuthenticationType = AuthenticationTypes.ServerBind | AuthenticationTypes.SecureSocketsLayer

希望这能有所帮助!

我不得不挖掘一个为一个客户定制的旧DLL项目。

我设法使它发挥作用。如果您有一个带有转义符的DN,那么您似乎必须引用这些低级目录服务例程。(请注意,在现实生活中,DN是通过设置DirectorySearcher并首先执行FindOne的初始用户搜索获得的)

 string userDn = @"cn=Feat Studentl+umanroleid=302432,ou=Faculty of Engineering & Physical Sciences Administration,ou=Faculty of Engineering & Physical Sciences,ou=People,o=University of TestSite,c=GB";
 string basicUrl = @"surinam.testsite.ac.uk:636";

  var ldapConnection = new LdapConnection(basicUrl);
  ldapConnection.AuthType = AuthType.Basic;
  LdapSessionOptions options = ldapConnection.SessionOptions;
  options.ProtocolVersion = 3;
  options.SecureSocketLayer = true;
  NetworkCredential credential = new NetworkCredential(userDn, password);                             
  ldapConnection.Credential = credential;
  try
  {
      ldapConnection.Bind();
      Console.WriteLine("bind succeeded ");
  }
  catch (LdapException e)
  {
      if (e.ErrorCode == 49)
      {
           Console.WriteLine("bind failed ");
      }
      else
      {
          Console.WriteLine("unexpected result " + e.ErrorCode);
      }
  }
  catch (DirectoryOperationException e)
  {
      Console.WriteLine("unexpected error " + e.Message);
  }