Skip to content

Instantly share code, notes, and snippets.

@mduheaume
Created May 13, 2011 17:52
Show Gist options
  • Save mduheaume/970987 to your computer and use it in GitHub Desktop.
Save mduheaume/970987 to your computer and use it in GitHub Desktop.
Check if a user belongs to an AD group
using System.DirectoryServices.AccountManagement;
try
{
string adGroupName = "ACTIVE_DIR_GROUP_NAME";
string userAccount = "USER_ACCOUNT_NAME"
using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal permissionGroup = GroupPrincipal.FindByIdentity(ctx, adGroupName);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, userAccount);
return userPrincipal.IsMemberOf(permissionGroup);
}
}
catch (Exception e)
{
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment