Finds the Groups for this User Account
The GetGroups method returns all the groups that the user belongs to. The groups that the user belongs to are added to the collection using Add method. The collection of groups is returned by this method.
internal static ArrayList FindGroupsOfUser(string user)
{
// Creating the PrincipalContext
PrincipalContext principalContext = null;
try
{
principalContext = new PrincipalContext(ContextType.Domain, "fabrikam", "DC=fabrikam,DC=com");
}
catch (Exception e)
{
MessageBox.Show("Failed to create PrincipalContext. Exception: " + e);
Application.Exit();
}
ArrayList groups = null;
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, user);
if (userPrincipal != null)
{
PrincipalSearchResult<Principal> groupPrincipals = userPrincipal.GetGroups(principalContext);
if (groupPrincipals != null)
{
groups = new ArrayList();
foreach (GroupPrincipal group in groupPrincipals)
groups.Add(group.Name);
}
}
return groups;
}
See Also
Reference
System.DirectoryServices.AccountManagement
Concepts
About System.DirectoryServices.AccountManagement
Using System.DirectoryServices.AccountManagement
Send comments about this topic to Microsoft.
Copyright © 2008 by Microsoft Corporation. All rights reserved.