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 623479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:05:49+00:00 2026-05-13T19:05:49+00:00

If I want only administrator to access the action called ManagerUser, I know I

  • 0

If I want only administrator to access the action called “ManagerUser”, I know I can do this:

[Authorize( Roles = Constants.ROLES_ADMINISTRATOR )]
public ActionResult ManageUser( string id )
{
}

What if I want to give everyone access except to administrator? I do not want to write all roles up there on function :|.

Any recommendations/way outs?

  • 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-13T19:05:50+00:00Added an answer on May 13, 2026 at 7:05 pm

    You can create your own custom Authorize attribute, something like “AuthorizeAllExceptAdmin.” Within that class you would simply need to check whether or not the current user was an admin, and if they were reject it, otherwise accept it.

    Here’s a good tutorial, but you’ll probably end up with something like:

    public class AuthorizeAllExceptAdmin : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return !httpContext.User.IsInRole(Constants.ROLES_ADMINISTRATOR);
        }
    }
    

    Then your controller method becomes:

    [AuthorizeAllExceptAdmin] 
    public ActionResult SomethingOnlyNonAdminsCanDo() 
    { 
    } 
    

    Here’s an example of the custom attribute that takes in roles to deny.

    public class DoNotAuthorize : AuthorizeAttribute
    {
        private IEnumerable<string> _rolesToReject;
    
        public DoNotAuthorize(IEnumerable<string> rolesToReject)
        {
            _rolesToReject = rolesToReject;        
        }
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            foreach (var role in _rolesToReject)
            {
                if (httpContext.User.IsInRole(role))
                    return false;
            }
    
            return true;
        }
    }
    

    Then your controller method becomes:

    [DoNotAuthorize(new [] {Constants.ROLES_ADMINISTRATOR})] 
    public ActionResult SomethingOnlyNonAdminsCanDo() 
    { 
    } 
    

    I would put some thought into it before choosing one of the above options. If you think you’ll have several methods (or entire controllers) with similar authorization requirements (i.e, several actions an admin can not perform) then I would stick with the non-parameterized custom attribute. This way, you can evolve them all together (by only changing the custom attribute) later on. For example, maybe later on you want admins to be able to go into a special mode where they can perform these actions.

    Alternatively, if the autorization is more varied amongst the actions, then using the parameterized list makes sense, since they’ll evolve relatively independently.

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

Sidebar

Related Questions

I want only a (faculty) group of users to be able to access a
I only want this function to run if .toolbar li does not have the
I have a database in Access 2003 that I only want certain people to
I used HTTPService for reading xml, but I want only a particular xml field
I have an application with many views. I want only a couple of the
How to avoid multiple instances of windows form in c# ?? i want only
I only want log4net to keep let's say 10 days-worth of log files as
I only want a simple Splash Screen Example. Get the Code, Insert my picture,
We only want one instance of our app running at any one time. So
I only want a method to activate if the pixel that is clicked is

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.