Created
May 13, 2011 17:52
-
-
Save mduheaume/970987 to your computer and use it in GitHub Desktop.
Check if a user belongs to an AD group
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
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