AD group name with "/" character inflates my binding

I am getting memberOf property for my user using this code:

DirectorySearcher search = new DirectorySearcher(new DirectoryEntry(connectionString));
search.Filter=string.Format("(&(sAMAccountName={0})(objectClass=user))",userName);
SearchResult result = search.FirndOne();

      

So far so good. However, I have to get the cn value for each group the user is a member of: I do this in a loop over the memberOf property.

List<string> groupList = new List<string>();
DirectoryEntry user = result.GetDirectoryEntry();

foreach(string groupPath in user.Properties["memberOf"])
{
    DirectoryEntry groupBinding = new DirectoryEntry("LDAP://"+groupPath);
    DirectorySearcher groupSearch = new DirectorySearcher(groupBinding);
    DirectoryEntry gorupEntry = groupSearch.FindOne().GetDirectoryEntry();
    groupList.Add(groupEntry.Properties["cn"].Value.ToString()));
}

      

The problem is that when groupPath includes the '/' character, it groupSearch.FindOne()

throws an exception.

How do I escape the / character so that I can search for this group?

0


a source to share


1 answer


use backslash to escape like this /



Credit: http://www.rlmueller.net/CharactersEscaped.htm

+1


a source







All Articles