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

  • Home
  • SEARCH
  • 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 8985947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:26:12+00:00 2026-06-15T21:26:12+00:00

Setting an action to be allowed by only a specific user or role is

  • 0

Setting an action to be allowed by only a specific user or role is easy using the [Authorize] attribute. E.g.

[Authorize(Roles = "Administrator")]
public ActionResult Index()
{
  ...

However, I ran into a problem when I wanted the inverse. Is there a way using MVC framework features to allow all authenticated users except those specified by name or role?

The desired usage would be something akin to:

[DoNotAuthorize(Roles = "RestrictedUser")]
public ActionResult Index()
{
  ...
  • 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-06-15T21:26:13+00:00Added an answer on June 15, 2026 at 9:26 pm

    One fairly simple solution is to derive from the AuthorizeAttribute class and override its AuthorizeCore method, swapping its true/false logic.

    /// <summary>
    /// Authorizes any authenticated user *except* those who match the provided Users or Roles.
    /// </summary>
    public class DoNotAuthorizeAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// This is effectively a copy of the MVC source for AuthorizeCore with true/false logic swapped.
        /// </summary>
        /// <param name="httpContext">The HTTP context, which encapsulates all HTTP-specific information about an individual HTTP request.</param>
        /// <returns>true if the user is authorized; otherwise, false.</returns>
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            IPrincipal user = httpContext.User;
            if (!user.Identity.IsAuthenticated)
            {
                return false;
            }
    
            string[] usersSplit = SplitString(Users);
            if ((usersSplit.Length > 0) && usersSplit.Contains<string>(user.Identity.Name, StringComparer.OrdinalIgnoreCase))
            {
                return false;
            }
    
            string[] rolesSplit = SplitString(Roles);
            if ((rolesSplit.Length > 0) && rolesSplit.Any<string>(new Func<string, bool>(user.IsInRole)))
            {
                return false;
            }
    
            return true;
        }
    
        /// <summary>
        /// This is a direct copy of the MVC source for the internal SplitString method.
        /// </summary>
        /// <param name="original">The original string to split.</param>
        /// <returns>An array of strings.</returns>
        internal static string[] SplitString(string original)
        {
            if (string.IsNullOrWhiteSpace(original))
            {
                return new string[0];
            }
            return (from piece in original.Split(new[] { ',' })
                    let trimmed = piece.Trim()
                    where !string.IsNullOrEmpty(trimmed)
                    select trimmed).ToArray<string>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a simple FormPanel in GWT (com.google.gwt.user.client.ui.FormPanel). Once setting the action for
I am in the process of setting up user action logging for my PHP
I am setting the action attribute of the Form tag by following line $(#divIsInternational
In my TestClass action I am setting an action error using addActionError method. I
Other then setting UIGestureRecognizers in a Controller: UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:v action:@selector(handleSwipe:)];
I know about Allow Triggers to Fire Others server setting that allows the action
I'm seeing something of an oddity when setting a cookie... Action: string cookieName =
Setting onchange event for CheckBoxList using the following code doesn't work. chkListUserGroup.Attributes.Add(onchange, document.forms[0].isRecordModified.value='true';); How
Hi I'm setting up admin routing in CakePHP. This is my current route: Router::connect('/admin/:controller/:action/*',
I am trying to mimic the action of right-clicking on a folder, setting modify

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.