Friday, November 03, 2006 12:25 AM
by
Paul Wu
Security IsInRole
Here is some code snippet to see if a user belongs to a certain security group. The code below will work with local group as well as AD groups:
|
Works in ASP.NET:
//Case Insensitive bool t1 = Page.User.IsInRole("Everyone");
Works Everywhere:
//Case Sensitive static bool IsInRole(string roleName) { System.Security.Principal.WindowsIdentity id = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.IdentityReferenceCollection group = id.Groups; System.Security.Principal.IdentityReferenceCollection ntNames = group.Translate(typeof(System.Security.Principal.NTAccount));
foreach (System.Security.Principal.IdentityReference ir in ntNames) { if (ir.Value.Substring(ir.Value.LastIndexOf(@"\") + 1) == roleName) return true; } return false; } |
Comments
Anonymous comments are disabled
About Paul Wu
Paul Wu is a Principal Consultant @
RuleMasters. With over 17 years of experience in the software industry, Paul is a seasoned Software Architect and a Microsoft Certified Technical Specialist in BizTalk and SQL2005.