Get user group memberships from Active Directory

How can I get user group memberships from AD, preferably using the same template as me, to get the user department property as shown below? I found several examples, but the overlapping set of all sample methods is quite small and lacks the tightness and simplicity of this department query:

        var adServer = ConfigurationManager.AppSettings["adServer"] ?? "localhost";
        var remoteRoot = new DirectoryEntry(GetRootPath(adServer));
        var searcher = new DirectorySearcher(remoteRoot, string.Format("(SAMAccountName={0})", shortUserName));

        searcher.PropertiesToLoad.Add("Department");
        SearchResult result = null;
        result = searcher.FindOne();
+5
source share
1 answer

Are you using .NET 3.5? If so, it is very simple:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

string userName = "yourUser";

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);

PrincipalSearchResult<Principal> results = user.GetAuthorizationGroups();

, .GetAuthorizationGroups() , , , .

MSDN, .NET 3.5, AD.

.NET 2.0 ...

+6

All Articles