Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 293443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:16:20+00:00 2026-05-12T06:16:20+00:00

I have a controller and I want two roles to be able to access

  • 0

I have a controller and I want two roles to be able to access it. 1-admin OR 2-moderator

I know you can do [Authorize(Roles=”admin, moderators”)] but I have my roles in an enum. With the enum I can only authorize ONE role. I can’t figure out how to authorize two.

I have tried something like [Authorize(Roles=MyEnum.Admin, MyEnum.Moderator)] but that wont compile.

Someone once suggested this:

 [Authorize(Roles=MyEnum.Admin)]
 [Authorize(MyEnum.Moderator)]
 public ActionResult myAction()
 {
 }

but it doesn’t work as an OR. I think in this case the user has to be part of BOTH roles. Am I overlooking some syntax? Or is this a case where I have to roll my own custom authorization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:16:21+00:00Added an answer on May 12, 2026 at 6:16 am

    Try using the bit OR operator like this:

    [Authorize(Roles= MyEnum.Admin | MyEnum.Moderator)]
    public ActionResult myAction()
    {
    }
    

    If that doesn’t work, you could just roll your own. I currently just did this on my project. Here’s what I did:

    public class AuthWhereRole : AuthorizeAttribute
    {
        /// <summary>
        /// Add the allowed roles to this property.
        /// </summary>
        public UserRole Is;
    
        /// <summary>
        /// Checks to see if the user is authenticated and has the
        /// correct role to access a particular view.
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        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 && ((Is & role) != role))
                return false;
    
            return true;
        }
    }
    
    // Example Use
    [AuthWhereRole(Is=MyEnum.Admin|MyEnum.Newbie)]
    public ActionResult Test() {}
    

    Also, make sure to add a flags attribute to your enum and make sure they are all valued from 1 and up. Like this:

    [Flags]
    public enum Roles
    {
        Admin = 1,
        Moderator = 1 << 1,
        Newbie = 1 << 2
        etc...
    }
    

    The left bit shifting gives the values 1, 2, 4, 8, 16 and so on.

    Well, I hope this helps a little.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an action inside my controller class and I want two different routes
i have a character controller which jumps but while jumping i want to change
I have two functions in controller named step1 and step2. I want to pass
I have a controller setup to accept two vars: /clients/view/var1/var2 And I want to
I have controller with action new , and I want it to create ActiveRecord::Base
I have a controller with 3 hashes I want to combine into 1 object
I have this code in my controller and want to test this code line
I have a controller where in the constructor function, I want to check if
I have a model that uses acts_as_url and in my controller I want to
I have a request controller that is getting out of hand, and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.