Created
November 18, 2015 13:30
-
-
Save steve-codemunkies/3ff6e307b2ee9f4fde1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var username = "some.user.name"; | |
var password = "some.password"; | |
var entry = new DirectoryEntry("LDAP://rootDSE", username, password); | |
var nativeObject = entry.NativeObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var groups = new List<string>(); | |
var groupsRegexp = new Regex("OU=Groups,"); | |
var groupNameRegexp = new Regex("CN=(?<group>[\\w\\s]+)"); | |
var searchPath = "LDAP://" + _entry.Properties["defaultNamingContext"].Value.ToString(); | |
var search = new DirectorySearcher(searchPath) {Filter = $"(SAMAccountName={entry.Username})"}; | |
search.PropertiesToLoad.Add("memberOf"); | |
var result = search.FindOne(); | |
var propertyCount = result.Properties["memberOf"].Count; | |
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++) | |
{ | |
var dn = (string)result.Properties["memberOf"][propertyCounter]; | |
if (groupsRegexp.IsMatch(dn)) | |
{ | |
var matches = groupNameRegexp.Matches(dn); | |
foreach (Match match in matches) | |
{ | |
groups.Add(match.Groups["group"].Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment