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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:02:42+00:00 2026-05-13T20:02:42+00:00

I am using the Authorize attribute like this: [Authorize (Roles=Admin, User)] Public ActionResult Index(int

  • 0

I am using the Authorize attribute like this:

[Authorize (Roles="Admin, User")]
Public ActionResult Index(int id)
{
    // blah
}

When a user is not in the specified roles, I get an error page (resource not found). So I put the HandleError attribute in also.

[Authorize (Roles="Admin, User"), HandleError]
Public ActionResult Index(int id)
{
    // blah
}

Now it goes to the Login page, if the user is not in the specified roles.

How do I get it to go to an Unauthorized page instead of the login page, when a user does not meet one of the required roles? And if a different error occurs, how do I distinguish that error from an Unauthorized error and handle it differently?

  • 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-13T20:02:43+00:00Added an answer on May 13, 2026 at 8:02 pm

    Add something like this to your web.config:

    <customErrors mode="On" defaultRedirect="~/Login">
         <error statusCode="401" redirect="~/Unauthorized" />
         <error statusCode="404" redirect="~/PageNotFound" />
    </customErrors>
    

    You should obviously create the /PageNotFound and /Unauthorized routes, actions and views.

    EDIT: I’m sorry, I apparently didn’t understand the problem thoroughly.

    The problem is that when the AuthorizeAttribute filter is executed, it decides that the user does not fit the requirements (he/she may be logged in, but is not in a correct role). It therefore sets the response status code to 401. This is intercepted by the FormsAuthentication module which will then perform the redirect.

    I see two alternatives:

    1. Disable the defaultRedirect.

    2. Create your own IAuthorizationFilter. Derive from AuthorizeAttribute and override HandleUnauthorizedRequest. In this method, if the user is authenticated do a redirect to /Unauthorized

    I don’t like either: the defaultRedirect functionality is nice and not something you want to implement yourself. The second approach results in the user being served a visually correct “You are not authorized”-page, but the HTTP status codes will not be the desired 401.

    I don’t know enough about HttpModules to say whether this can be circumvented with a a tolerable hack.

    EDIT 2:
    How about implementing your own IAuthorizationFilter in the following way: download the MVC2 code from CodePlex and “borrow” the code for AuthorizeAttribute. Change the OnAuthorization method to look like

        public virtual void OnAuthorization(AuthorizationContext filterContext)
        {
            if (AuthorizeCore(filterContext.HttpContext))
            { 
                HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                cachePolicy.SetProxyMaxAge(new TimeSpan(0));
                cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
            }
            // Is user logged in?
            else if(filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                // Redirect to custom Unauthorized page
                filterContext.Result = new RedirectResult(unauthorizedUrl);
            } 
            else {
                // Handle in the usual way
                HandleUnauthorizedRequest(filterContext);
            }
        }
    

    where unauthorizedUrl is either a property on the filter or read from Web.config.

    You could also inherit from AuthorizeAttribute and override OnAuthorization, but you would end up writing a couple of private methods which are already in AuthorizeAttribute.

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

Sidebar

Ask A Question

Stats

  • Questions 356k
  • Answers 356k
  • 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 Which HTML element is the target of the event? (on… May 14, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer You are correct; the using statement compiles to a try… May 14, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer Rather than using percentages, you can define the #centerdiv using… May 14, 2026 at 8:49 am

Related Questions

Using a technique I found in one of the recent ASP.NET MVC books, I
I come to the conclusion I need to ditch the ASP.NET Membership (for list
I'm trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute
I'm brand new at Python and I'm trying to write an extension to an
Lets say I have an ASP.Net MVC App and this app (UI) references a

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.