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

Ask A Question

Stats

  • Questions 261k
  • Answers 261k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I posted a demo of what I think you want… May 13, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer Most assertions take a message parameter, for example: assert_equal 64,… May 13, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer Whether it's an unwanted copy or not is for you… May 13, 2026 at 11:38 am

Related Questions

I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This
I have a question regarding how to get the white- and black-listing feature of
I'm currently building a web application (utilizing Zend Framework) and have gotten some of
I'm working on a Rails web application, and it's currently being used by some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.