I have asp.net web app, how to check the current logged in user (client) is in specific Active directory group. Thanks
-
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);Paul Blackwell– Paul Blackwell2013-02-07 03:29:59 +00:00Commented Feb 7, 2013 at 3:29
Add a comment
|
2 Answers
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;
}
7 Comments
Yogesh
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 ?
Leniel Maccaferri
@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...
marc_s
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.
marc_s
@Yogesh: where (on what line in the code) does this error happen when you debug through??
Yogesh
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
|