次の方法で共有


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