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; } |