猿问

ishedNameEncode 如何与 C# DirectoryServices 库一起使用?

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


AntiXSSLdapDistinguishedNameEncode似乎是为此定制的,但是当我尝试在 OU 的名称上使用它时,将其插入到可分辨名称中,然后将其传递到一个PrincipalContext或DirectoryEntry对象中,我得到无法找到该对象的异常。


使用未经清理的 OU 名称时,一切都会成功。


AD 库是否理解编码的专有名称?还是预期在创建 OU之前以及稍后引用它时LdapDistinguishedNameEncode运行?


示例代码,ouName“Bob-Marley”或“dlanod'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);

        }


叮当猫咪
浏览 219回答 2
2回答

吃鸡游戏

我建议你试试LdapDistinguishedNameEncode(string, bool, bool)超载。取自AntiXSS 4.0 Released:还提供了LdapDistinguishedNameEncode(string, bool, bool),因此您可以关闭初始或最终字符转义规则,例如,如果您将转义的专有名称片段连接到完整专有名称的中间。希望能帮助到你!

慕斯王

我没有找到仍然让我使用 LdapDistinguishedNameEncode 的可行方法。我采用的代码是:&nbsp; &nbsp; &nbsp; &nbsp; // Reject organization names with illegal or problematic characters (taken from RFC2253).&nbsp; &nbsp; &nbsp; &nbsp; // Used instead of Encoder.LdapDistinguishedNameEncode because of an expectation that&nbsp; &nbsp; &nbsp; &nbsp; // the creation passes through the same function.&nbsp; &nbsp; &nbsp; &nbsp; // https://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx&nbsp; &nbsp; &nbsp; &nbsp; char[] illegalChars = {',', '\\', '#', '+', '<', '>', ';', '"', '='};&nbsp; &nbsp; &nbsp; &nbsp; if (name.IndexOfAny(illegalChars) >= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答