Thursday, March 15, 2012 Check if user is in Active Directory Group

Here is a simple function to see if a user is in a AD group.

Be sure to add the Directory Services as a refrence to your project and use:

using System.DirectoryServices.AccountManagement;

        public string GetDomain()

            return Environment.UserDomainName.ToString();


        public bool IsInGroup(string ingroup)
            string username = Environment.UserName;

            var domainctx = new PrincipalContext(ContextType.Domain, GetDomain());
            var userPrincipal = UserPrincipal.FindByIdentity(domainctx, IdentityType.SamAccountName, username);

            bool isMember = userPrincipal.IsMemberOf(domainctx, IdentityType.Name, ingroup);

            return isMember;