VB.NET - Remove CN= from user's distinguished name.

kouts1
kouts1 used Ask the Experts™
on
Hi!

I need to remove CN= form a distinguished name.  How do I go about parsing the DN.

what I have at the moment is CN=, OU=, DC=, DC=.  I need to kepp everything from OU= and on.  Any help would be appreciated! examples in vb.net or c# would be fine.

Thanks,
Kouts1
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
string t = Regex.Replace(tnput, @",CN=[^,]", "", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

Commented:
Forgot a "*".  Use this:
    string t = Regex.Replace(input, @",CN=[^,]*", "",  RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

Commented:
please try the following code,

public void RemoveUserFromGroup(string userDn, string groupDn)
{
    try
    {
        DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
        dirEntry.Properties["member"].Remove(userDn);
        dirEntry.CommitChanges();
        dirEntry.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //doSomething with E.Message.ToString();

    }
}
Commented:
If there's no comma before the CN=, you should probably use
   string t = Regex.Replace(input, @"CN=[^,]*,* *", "",   RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

Chris DentPowerShell Developer
Top Expert 2010

Commented:

Just to be annoying... Names (CN=) may in some cases contain ,. It has to be escaped in the DN, but it's still possible.

Chris

Commented:
@Chris:

Can you please clarify with an example?  Are you referring to some escape sequence other than "%2c"?
Chris DentPowerShell Developer
Top Expert 2010
Commented:

Sure:

CN=Dent\, Chris,OU=somewhere,DC=domain,DC=com

or is that %2c?

Chris

Commented:
I thought special characters in URLs were encoded with % (http://www.blooberry.com/indexdot/html/topics/urlencoding.htm), but thought you might have seen it done otherwise.  (AWK)
Chris DentPowerShell Developer
Top Expert 2010

Commented:
\ is the escape character for LDAP Distinguished Names. We can refer to the RFC for the full list (2.4):

http://www.ietf.org/rfc/rfc2253.txt

Chris

Commented:
@Chris:  Thanks. Very messy.

Commented:
@kouts1:  According to the RFC provided by Chris, these strings can be pathologically complex.  Do you need a fully general solution (lots of strange punctuation, escapes and quotes), or do you have reasonably well behaved data?  Can you provide some real examples?

Author

Commented:
I ended up doing this and it worked ok,  Not a great solution but at least I got the CN= removed from the DN.

NewUser.MoveTo(New DirectoryEntry("LDAP://" & TxtuserDN2.Text.Split(",")(1) & "," & "DC=mmcactdom,DC=mmcroot,DC=mmc"))
Basically  TxtuserDN2.Text.Split(",")(1) gave me this:  OU=DepName
SO i added "," and the rest of the domain string.
Final result gave me exactly what I needed: OU=DepName,DC=,DC=, DC=
Not great but works!

kouts1

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial