Directory Programming .NET

Active Directory and ADAM programming support for .NET developers
Welcome to Directory Programming .NET Sign in | Join | Help
in Search

Please help me filter a group which group have special character

Last post 09-30-2010, 4:04 PM by chris128. 4 replies.
Sort Posts: Previous Next
  •  08-19-2010, 10:54 PM 8517

    Please help me filter a group which group have special character

    Hi Everybody,
    I wrote the code to search a group as follows:
    ....
    directorySearcher.Filter = "(&(objectClass=group)(sAMAccountName=" +  ReplaceSpecialCharInAD(groupName) + "))";
    .....
     public string ReplaceSpecialCharInAD(string strSource)
            {
                string str = "";
                str = strSource;
                str = str.Replace(@"\", @"\5C");           
                str = str.Replace(@";", @"\5C\3B");
                str = str.Replace("*", @"\2A");
                str = str.Replace("\"", @"\5C\22");
                str = str.Replace("(", @"\28");
                str = str.Replace(")", @"\29");
                str = str.Replace(" NUL ", @" \00 ");
                str = str.Replace("/", @"\2F");
                return str;
            }
    If I create a group in AD without special characters, it always worked fine but if I create a group with special characters (example: Group 2 / 5 ~!@#$%^&*(() _ +) it does not always find this group even exists in the domain.

    Any ideas on how to work around this? Please help me.
    Thanks,

    Aladin

  •  09-16-2010, 8:16 PM 8555 in reply to 8517

    Re: Please help me filter a group which group have special character

    I suspect because of your ordering you are double and triple escaping things.  On the first replace you are adding the same char you just replaced back into the string to be escaped yet again.

    Anyhow, if you follow along for escaping filters (as opposed to DNs) here, it should be fine.

    Ryan Dunn
    Extemporaneous Mumblings
    The .NET Developer's Guide to Directory Services Programming
  •  09-17-2010, 3:55 AM 8557 in reply to 8517

    Re: Please help me filter a group which group have special character

    Well when you create a group and give it a name with special characters, you get told that the pre 2000 name (which is the sAMAccountName attribute) will have some special characters converted to an underscore. So you need to bear that in mind as well when searching via the sAMAccountName attribute.

    Taking your example group name, Group 2 / 5 ~!@#$%^&*(() _ +)
    it would end up as this in the sAMAccountName attribute:
    Group 2 _ 5 ~!@#$%^&_(() _ _)
    so that is what you need to search for.

    Of course there are still characters in that name that we cannot have in a filter, so you can just do this (I'm a VB.NET person but hopefully its easy for you to convert) but I believe this is basically what you are already doing anyway, so maybe its just the above point about replacing some special characters with a _ character first that you need to take into account.


    E.g

    'This is the account name with _ characters already in it where they need to be
    Dim sAMAccountName As String = "Group 2 _ 5 ~!@#$%^&_(() _ _)"
    sAMAccountName = sAMAccountName.Replace("\", "\5c").Replace("*", "\2a").Replace("(", "\28").Replace(")", "\29").Replace("/", "\2f")
    searcher.Filter = "(sAMAccountName=" & sAMAccountName & ")"
    My website: cjwdev.co.uk
    My blog: cjwdev.wordpress.com
  •  09-22-2010, 11:04 PM 8578 in reply to 8557

    Re: Please help me filter a group which group have special character

    Thank you so much for help me, I resolved the problem.
  •  09-30-2010, 4:04 PM 8597 in reply to 8578

    Re: Please help me filter a group which group have special character

    No problem, glad you got it sorted :)
    My website: cjwdev.co.uk
    My blog: cjwdev.wordpress.com
View as RSS news feed in XML