独自の拡張メソッドを作成して、コードで使用できます。
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));
}
}
この拡張メソッドを次のように呼び出すだけです。
// 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
}
これらの拡張メソッドをビューでも使用できますが、ビューは簡単に単体テストできないため、アプリのロジックをできるだけビューに記述しないようにしてください。