简体   繁体   中英

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

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;
}

Try this (Only works in ASP.NET but similar calls are available for windows apps)

    if (HttpContext.Current.User.IsInRole("RoleName"))
    {
        return true;
    }
    else
    {
        return false;
    }

Hope this helps
Harvey Sather

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM