Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try using the bit OR operator like this:</p> <pre><code>[Authorize(Roles= MyEnum.Admin | MyEnum.Moderator)] public ActionResult myAction() { } </code></pre> <p>If that doesn't work, you could just roll your own. I currently just did this on my project. Here's what I did:</p> <pre><code>public class AuthWhereRole : AuthorizeAttribute { /// &lt;summary&gt; /// Add the allowed roles to this property. /// &lt;/summary&gt; public UserRole Is; /// &lt;summary&gt; /// Checks to see if the user is authenticated and has the /// correct role to access a particular view. /// &lt;/summary&gt; /// &lt;param name="httpContext"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext == null) throw new ArgumentNullException("httpContext"); // Make sure the user is authenticated. if (!httpContext.User.Identity.IsAuthenticated) return false; UserRole role = someUser.Role; // Load the user's role here // Perform a bitwise operation to see if the user's role // is in the passed in role values. if (Is != 0 &amp;&amp; ((Is &amp; role) != role)) return false; return true; } } // Example Use [AuthWhereRole(Is=MyEnum.Admin|MyEnum.Newbie)] public ActionResult Test() {} </code></pre> <p>Also, make sure to add a flags attribute to your enum and make sure they are all valued from 1 and up. Like this:</p> <pre><code>[Flags] public enum Roles { Admin = 1, Moderator = 1 &lt;&lt; 1, Newbie = 1 &lt;&lt; 2 etc... } </code></pre> <p>The left bit shifting gives the values 1, 2, 4, 8, 16 and so on.</p> <p>Well, I hope this helps a little.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload