I've got a string that I'm fetching from LDAP for Active Directory group membership and I need to parse it to check if the user is a member of the AD group. Is there a class that can parse this for me?
Example:
CN=Foo Group Name,DC=mydomain,DC=com
I've got a string that I'm fetching from LDAP for Active Directory group membership and I need to parse it to check if the user is a member of the AD group. Is there a class that can parse this for me?
Example:
CN=Foo Group Name,DC=mydomain,DC=com
I came here to see if we can parse "LDAP://ldap.company.com:389/ou=people,o=company" to protocol, port, baseDN and server FQDN. I tried System.Uri class it worked as excepted.
To answer the parsing question, use PInvoke with
DsGetRdnW
. For code, see my answer to another question: https://stackoverflow.com/a/11091804/628981.But it sounds like you're doing it wrong. First, get the SID for your target group:
Then, it depends on what you have. If the user is running your app (or authenticated to your website/service), then enumerate the SIDs in the token. For example, in desktop apps use
WindowsIdentity.GetCurrent().Groups
. Otherwise, you'll need to get a DirectoryEntry for the user and then get thetokenAttributes
attribute like spoulson suggested:Just in case you need to get a DirectoryEntry from a SID, you can get the search string by:
Besides, if you query the AD for a group members, you'll be able to compare all of the members' distinguishedName's directly without parsing code through the
DirectoryEntry
class of theSystem.DirectoryServices
namespace.Otherwise, I just don't know of such a class somewhere. =)
Hope this helps anyway somehow !
EDIT #1
Here's a link from which I have learned a lot working with the AD and the
System.DirectoryServices
namespace:Howto: (Almost) Everything In Active Directory via C#
I shall provide you with a sample code in a few days, if you still require it, where I will use the
System.DirectoryServices.DirectorySearcher
object class to retrieve the members of a group.I hope this link will help you as it did for me! =)
EDIT #2
Here's the code sample I told you about. This should make it more efficient to query against the AD without having to work bakc and forth the AD.
To parse the DistinquishedName you have to pay attention to the escape characters. Here's a method that will parse the string correctly and return a list of key value pairs.
If you don't want to add additional dependencies and just want to parse the string..
This type of string can easily be parsed just using string.Split. To get the CN values, would be something like..
These are called distinguished names.
CodeProject has a parser project that appears to do what you need: http://www.codeproject.com/KB/IP/dnparser.aspx