Search code examples
c#asp.netactive-directoryactive-directory-group

asp.net web app - check user exist in Active Directory group


I have asp.net web app, how to check the current logged in user (client) is in specific Active directory group. Thanks


Solution

  • Try this the following method. Just change it according to your needs...

    public List<string> GetGroupNames(string userName)
    {
        var pc = new PrincipalContext(ContextType.Domain);
        var src = UserPrincipal.FindByIdentity(pc, userName).GetGroups(pc);
        var result = new List<string>();
        src.ToList().ForEach(sr => result.Add(sr.SamAccountName));
        return result;
    }