Getting user roles/groups from Active Directory -

2019-09-07 01:36发布

问题:

I need to be able to compare the users active directory groups against a list of acceptable groups. This is not for authentication.

I know i can use System.DirectoryServices but i have seen many posts that say to use AccountManagement but all i see is .Active Directory.

Can anyone please assist me in the right direction?

回答1:

Try something like this: check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

  • Managing Directory Security Principals in the .NET Framework 3.5
  • MSDN docs on System.DirectoryServices.AccountManagement

Basically, you can define a domain context and easily find users and/or groups in AD:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   PrincipalSearchResult<Principal> authgroups = user.GetAuthorizationGroups();

   // do your checking with the auth groups that the user has - against your list 
}

The new S.DS.AM makes it really easy to play around with users and groups in AD!