2

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

1
  • Great answer but for future reference received {"Unknown error (0x80005000)"} when implemented. Fixed by adding domain to following line: var pc = new PrincipalContext(ContextType.Domain, Environment.UserDomainName); Commented Feb 7, 2013 at 3:29

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Leniel, Thanks for the quick reply. i just copied your code ran on simple web app. this gave an error - {"Unknown error (0x80005000)"} , am i missing anything ?
@Yogesh: check this: lansweeper.com/forum/… This is a pretty broad error. If you search for this Unknown error in Google you'll get a lot of possible problems...
This requires .NET 3.5 or higher - doesn't work on 2.0 or 3.0. Also: the user that the web app is running under needs to have permission to at least read the AD.
@Yogesh: where (on what line in the code) does this error happen when you debug through??
now I used exisitng group where i am the member and i used this line of code flag = User.IsInRole("ADGroupName"); .... Bingo! and it worked
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.