title: How can i format an LDAP Filter that includes special characters? ('Classic' ASP)
body: <p>I'm having trouble retrieving information via LDAP for certain groups I have the DistinguishedName of.
  The issue seems to relate to them having special characters.</p>
  
  <p>Here are two examples, one working, one not:<br>
  All in Test Group<br>
  All in 463\"567y\\22\"¤&amp;/2#%&amp;! Test Group<br></p>
  
  <p>and their dn's:<br>
  CN=All in Test Group,OU=Groups,DC=some,DC=test,DC=com<br>
  CN=All in 463\"567y\\22\"¤&amp;/2#%&amp;! Test Group,OU=Groups,DC=some,DC=test,DC=com<br></p>
  
  <p>I know the dn's are correct, as I retrieve them from a users managedObjects attribute, and have verified them in AD and also using ADSI Edit.</p>
  
  <p>Now, onto what code I am using to retrieve the information, note that this code works fine on the group without special characters:</p>
  
  <pre><code>Dim strGroupdisplayName, strGroupsAMAccountname, strGroupmail
  
  
  Function GetGroupInfofromDN(group_str)
  on error resume next
  DIM objGroup, objDNNamespace, strLDAPGroup
  strLDAPGroup = "LDAP://" + group_str
  Set objDNNamespace = GetObject("LDAP:")
  Set objGroup = objDNNamespace.OpenDSObject(strLDAPGroup, strADUsername, strADPassword,0)
  objGroup.GetInfo
  strGroupdisplayName = ""
  strGroupsAMAccountname = ""
  strGroupmail = ""
  strGroupdisplayName = ObjGroup.Get("displayName")
  strGroupsAMAccountname = ObjGroup.Get("sAMAccountname")
  strGroupmail = ObjGroup.Get("mail")
  set objGroup = Nothing
  End Function
  </code></pre>
  
  <p>As for what I've tried... I've tried encoding the groups to URI format, I've tried replacing special characters with their escaped equivalents:</p>
  
  <pre><code>strTemp = replace(strTemp, "\", "\5c")
  strTemp = replace(strTemp, "(", "\28")
  strTemp = replace(strTemp, "|", "\7c")
  strTemp = replace(strTemp, "&lt;", "\3c")
  strTemp = replace(strTemp, "/", "\2f")
  strTemp = replace(strTemp, ")", "\29")
  strTemp = replace(strTemp, "=", "\3d")
  strTemp = replace(strTemp, "~", "\7e")
  strTemp = replace(strTemp, "&amp;", "\26")
  strTemp = replace(strTemp, "&gt;", "\3e")
  strTemp = replace(strTemp, "*", "\2a")
  </code></pre>
  
  <p>I've also tried via regex to pull out the CN= section and only alter that.</p>
  
  <p>Quite frankly, i'm at a loss as to what I should do here.</p>
  
  <p>I've also tried another method:</p>
  
  <pre><code>set connAD = Server.CreateObject("ADODB.Connection")
  connAD.Provider = "ADsDSOObject"
  connAD.Properties("User ID") = strADUsername 
  connAD.Properties("Password") = strADPassword
  connAD.Properties("Encrypt Password") = true
  connAD.Open
  
  Function getADUserInfo(strUID)
  
      strGeneralLookupError = false
      strBase = "&lt;LDAP://DC=SOME,DC=TEST,DC=COM&gt;"
      strFilter = "(distinguishedName=" &amp; strUID &amp; ")" 
      strAttributes = "cn, mail, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber, distinguishedName, managedObjects"
      strScope = "subtree"    
      strFullCommand = strBase &amp; ";" &amp; strFilter &amp; ";" &amp; strAttributes &amp; ";" &amp; strScope
      set rsADUserInfo = Server.CreateObject("ADODB.Recordset")
      set rsADUserInfo = connAD.Execute(strFullCommand)
      set getADUserInfo = rsADUserInfo
      set rsADUserInfo = Nothing
  End Function
  
  Sub getUserData(p_strUserID)
  
      strADLookupSuccess = true
      set rsUserData = Server.CreateObject("ADODB.Recordset")
      set rsUserData = getADUserInfo(p_strUserID)
      if not rsUserData.EOF then
          strUserADsPath = rsUserData("ADsPath")
          strUserdistinguishedName = rsUserData("distinguishedName")
      else
          strADLookupSuccess = false
      end if
      rsUserData.Close
      set rsUserData = Nothing
  End Sub
  
  dim strUserADsPath, strUserdistinguishedName, rsUserData, rsADUserInfo, strADLookupSuccess
  getUserData("CN=All in 463\"567y\\\\22\"¤&amp;/2\#%&amp;! Test Group,OU=Groups,DC=some,DC=test,DC=com")
  
  connAD.Close
  set connAD = Nothing
  </code></pre>
  
  <p>Any suggestions? All the things I've read so far make mention to special characters, but escaping them does not seem to work...</p>
  
  <p>Also, this is Classic ASP, running against Windows Server 2008 r2-based domain.</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <blockquote>
    <p>Active Directory error '80040e37' </p>
    
    <p>An invalid directory pathname was passed</p>
  </blockquote>
  
  <p>Is the error message given when I do manage to pass one with Special Characters.</p>