I get the 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 then have to get the cn value for each group the user is a member of: I do that 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 issue is that when the groupPath includes a '/' character the groupSearch.FindOne()
throws an exception.
How do I escape the / character so that I can search for that group?