<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Active Directory</title>
        <link>http://blogs.imeta.co.uk/TPeplow/category/54.aspx</link>
        <description>Active Directory</description>
        <language>en-GB</language>
        <copyright>Tom Peplow</copyright>
        <generator>Subtext Version 2.1.1.1</generator>
        <item>
            <title>LDAP Distinguished Names escaping illegal characters</title>
            <link>http://blogs.imeta.co.uk/TPeplow/archive/2008/07/03/300.aspx</link>
            <description>&lt;p&gt;I needed to escape illegal characters within an LDAP query but couldn't find anything out there to do it.  The illegal characters are defined here: &lt;font face="Arial"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa366101.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa366101.aspx&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Say I want to query for a group called "Test/Group" the LDAP would need to look something like &lt;a href="ldap://CN=Test\/Group,OU=a,DC=x,DC=y"&gt;LDAP://CN=Test\/Group,OU=a,DC=x,DC=y&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I came up with this regular expression to clean it up.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;(?&amp;lt;=(?:[^\\]|^)(\\\\)+|[^\\]|^)[/,+\"&amp;gt;&amp;lt;;=#]|(?&amp;lt;=(?:[^\\]|^)(\\\\)+|[^\\]|^)\\(?!\\|[/,+\"&amp;gt;&amp;lt;;=#]| $|(?&amp;lt;=^\\) )|^ &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;So that translated into code a bit like "&lt;a href="ldap://CN=/"&gt;LDAP://CN=&lt;/a&gt;" + &lt;font color="#2b91af" size="2"&gt;Regex&lt;/font&gt;&lt;font size="2"&gt;.Replace(name, &lt;/font&gt;&lt;font color="#a31515" size="2"&gt;"(?&amp;lt;=(?:[^\\\\]|^)(\\\\\\\\)+|[^\\\\]|^)[/,+\"&amp;gt;&amp;lt;;=#]|(?&amp;lt;=(?:[^\\\\]|^)(\\\\\\\\)+|[^\\\\]|^)\\\\(?!\\\\|[/,+\"&amp;gt;&amp;lt;;=#]| $|(?&amp;lt;=^\\\\) )|^ "&lt;/font&gt;&lt;font size="2"&gt;, "&lt;/font&gt;&lt;font size="2"&gt;&lt;a href="file://$0/"&gt;&lt;font size="+0"&gt;\\$0&lt;/font&gt;&lt;/a&gt;"&lt;/font&gt;&lt;font size="2"&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;One thing I did notice was that when you ask for a users groups it is kind enough to escape most of the illegal characters for you, the only one it doesn't do is the /.  So if you're forming up an LDAP query straight from the memberOf property then all you need to do is make sure that the / is replaced with a \/...  Which makes life a bit easier and you don't need the regular expression then.  For example:&lt;/font&gt;&lt;/p&gt;
&lt;font color="#2b91af" size="2"&gt;
&lt;p&gt;SearchResult &lt;/p&gt;
&lt;/font&gt;&lt;font size="2"&gt;user = srch.FindOne();&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;foreach&lt;/font&gt;&lt;font size="2"&gt; (&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt; memberOf &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;in&lt;/font&gt;&lt;font size="2"&gt; user.Properties[&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;"memberOf"&lt;/font&gt;&lt;font size="2"&gt;])&lt;br /&gt;
{&lt;br /&gt;
    &lt;font color="#2b91af" size="2"&gt;DirectoryEntry &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;group = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;DirectoryEntry&lt;/font&gt;&lt;font size="2"&gt;(&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;"LDAP://"&lt;/font&gt;&lt;font size="2"&gt; + memberOf.Replace("/", "\/"));&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;img src="http://blogs.imeta.co.uk/TPeplow/aggbug/300.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Tom Peplow</dc:creator>
            <guid>http://blogs.imeta.co.uk/TPeplow/archive/2008/07/03/300.aspx</guid>
            <pubDate>Thu, 03 Jul 2008 13:39:51 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/TPeplow/comments/300.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/TPeplow/archive/2008/07/03/300.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/TPeplow/comments/commentRss/300.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/TPeplow/services/trackbacks/300.aspx</trackback:ping>
        </item>
    </channel>
</rss>