代码之家  ›  专栏  ›  技术社区  ›  dlanod

LdapDistinguishedNameEncode如何与C#DirectoryServices库一起工作?

  •  0
  • dlanod  · 技术社区  · 7 年前

    我正在尝试为C应用程序清除用户输入的OU名称。

    LdapDistinguishedNameEncode 似乎是为此而定制的,但当我尝试在OU的名称上使用此名称时,请将其插入到可分辨名称中,并将其传递到 PrincipalContext DirectoryEntry

    当使用未消毒的OU名称时,它都能成功工作。

    广告库是否理解编码的可分辨名称?还是期望 LdapDistinguishedNameEncode编码 是用来跑的 之前 OU创建以及以后引用它的时候?

    示例代码,其中 ouName 是“Bob Marley”还是“Dlando's company”以及破折号和撇号被编码:

            var distinguishedName = $"OU={Encoder.LdapDistinguishedNameEncode(ouName)},DC=sample,DC=net";
    
            try
            {
                // Create a context for the OU
                var context = new PrincipalContext(ContextType.Domain, _targetedUrl, distinguishedName, ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing);
            }
            catch (Exception e)
            {
                _logger.Error(e, "Organization {0} with definition {1} could not be found, exception occurred {2}", ouName, distinguishedName, e);
            }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Itay Podhajcer    7 年前

    我建议你试试 LdapDistinguishedNameEncode(string, bool, bool) AntiXSS 4.0 Released :

    还提供了,因此可以关闭初始或最终字符转义规则,例如,如果要将转义的可分辨名称片段连接到完整的可分辨名称中间。

    希望有帮助!

        2
  •  0
  •   dlanod    5 年前

    我采用的准则是:

            // Reject organization names with illegal or problematic characters (taken from RFC2253).
            // Used instead of Encoder.LdapDistinguishedNameEncode because of an expectation that
            // the creation passes through the same function.
            // https://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx
            char[] illegalChars = {',', '\\', '#', '+', '<', '>', ';', '"', '='};
            if (name.IndexOfAny(illegalChars) >= 0)
            {
                return false;
            }