You can write your own extension method and use it in your code.
public static class PrincipalExtensions { public static bool IsInAllRoles(this IPrincipal principal, params string[] roles) { return roles.All(r => principal.IsInRole(r)); } public static bool IsInAnyRoles(this IPrincipal principal, params string[] roles) { return roles.Any(r => principal.IsInRole(r)); } }
Now you can simply call this extension method as follows:
// user must be assign to all of the roles if(User.IsInAllRoles("Admin","Manager","YetOtherRole")) { // do something } // one of the roles sufficient if(User.IsInAnyRoles("Admin","Manager","YetOtherRole")) { // do something }
Although you could use these extension methods in your views, try to avoid writing application logic in the view as much as possible, since views cannot be easily tested.
Sam Farajpour Ghamari
source share