title: How to escape a string in C#, for use in an LDAP query
body: <p>I have an LDAP query, which I am using to perform a search in C#. It uses two string variables (username and domain) which need to be escaped for security reasons.</p>
  
  <p>How should I escape the strings? Is there a function available in C#.NET to do this?</p>
  
  <hr>
  
  <p>Example LDAP search conditions :</p>
  
  <pre><code>(objectCategory=person)
  (userprincipalname=username@domain*)
  (samaccountname=username)
  </code></pre>
  
  <p>Example LDAP query string in C# :</p>
  
  <pre><code>string search = "(&amp;(&amp;(objectCategory=person)(userprincipalname=" 
          + username 
          + "@"
          + domain 
          + "*)(samaccountname=" 
          + username 
          + ")))";
  </code></pre>
  
  <p>Edit: I already have the LDAP query working, and returning results. All I want is to escape the parameters.</p>