How to check if a user belongs to a Security or Distribution group in ASP.Net
If you need to check at Runtime if your User belongs to a Security group. Here's how to do it -
Assuming the IIS Application has Authentication set to "Windows Authentication". The Request object of HttpContext has the Logged in users Identity which contains Groups. Here is the code which validates if a user belongs to a particular Security group -
var
account = new NTAccount(@"Domain\SecurityGroupName"); //The security group you want to check the user belongs to
var
groups = HttpContext.Current.Request.LogonUserIdentity.Groups;//Get a collection of Groups the user belongs to
bool
hasAccount = groups.Contains(account.Translate(typeof(SecurityIdentifier)));//do the test
Comments
Anonymous
March 25, 2009
PingBack from http://blog.a-foton.ru/index.php/2009/03/26/how-to-check-if-a-user-belongs-to-a-security-or-distribution-group-in-aspnet/Anonymous
May 04, 2011
How to check if some not currently logged on user is in some group or not? Thanks.