2
Vote

Queries fail when using special characters

description

I had to search for displayName's which started with a "(". LDAP query syntax uses parenthesis and therefore can't handle this without replacing that and other special characters with the escape sequence substitutes (http://msdn.microsoft.com/en-us/library/aa746475%28VS.85%29.aspx)
 
I added the following function to my copy of DirectorySource.cs
 
    /// <summary>
    /// Replaces the special characters.
    /// </summary>
    /// <param name="constantValue">The constant value.</param>
    /// <returns></returns>
    private object ReplaceSpecialCharacters(object constantValue)
    {
        if (constantValue is string)
        {
            StringBuilder returnValue = new StringBuilder((string)constantValue);
            returnValue = returnValue.Replace(@"\", @"\5c");
            returnValue = returnValue.Replace(@"*", @"\2a");
            returnValue = returnValue.Replace(@"(", @"\28");
            returnValue = returnValue.Replace(@")", @"\29");
            returnValue = returnValue.Replace(@"/", @"\00");
 
            return returnValue.ToString();
        }
        return constantValue;
    }
 
And then modified ParsePredicate to contain the following:
 
                switch (m.Method.Name)
                {
                    case "Contains":
                        {
                            ConstantExpression c = m.Arguments[0] as ConstantExpression;
                            sb.AppendFormat("{0}=*{1}*", GetFieldName(o.Member), ReplaceSpecialCharacters(c.Value));
                            break;
                        }
                    case "StartsWith":
                        {
                            ConstantExpression c = m.Arguments[0] as ConstantExpression;
                            sb.AppendFormat("{0}={1}*", GetFieldName(o.Member), ReplaceSpecialCharacters(c.Value));
                            break;
                        }
                    case "EndsWith":
                        {
                            ConstantExpression c = m.Arguments[0] as ConstantExpression;
                            sb.AppendFormat("{0}=*{1}", GetFieldName(o.Member), ReplaceSpecialCharacters(c.Value));
                            break;
                        }
                    default:
                        throw new NotSupportedException("Unsupported string filtering query expression detected. Cannot translate to LDAP equivalent.");
                }

comments

bdesmet wrote Jul 18, 2010 at 4:00 AM

This will be fixed in the upcoming release.

wrote Aug 18, 2011 at 9:16 PM

wrote Feb 14, 2013 at 6:56 PM