0

Hi guys i m having a problem with CustomErrors in web.config, i'm trying to show a custom page for the error 401, but when a user try to access the controller and haven't authorization.

In this case the return is 302 but must be 401, i tried to use the Application_EndRequest() in Global.asax to change the Response.Status Code :

void Application_EndRequest(object sender, EventArgs e)
{
    if (Context.Response.StatusCode == 302)
    {
        Context.Response.Clear();
        Context.Response.StatusCode = 401;
    }
}

with this the error returned is the 401 but the page of custom errors is not displayed, just a default message of IIS

Unauthorized: Access is denied due to invalid credentials

how can i make the custom errors show the right page for 401 error ?

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
Lucas Konrath
  • 567
  • 2
  • 8
  • 19

3 Answers3

0

If you are using Forms Authentication, then 401 is handled internally by ASP.NET and used in the Challenge/Response mechanism for authentication. When you use authentication, you specify a login page, and when a user is not authorized, they are redirect to that page to log in.

There is a difference between a 401, which unauthorized and 403, which is Forbidden.

If you're using Windows Authentication, Digest, or Basic then you will get a 401 Challenge popup. ASP.NET intercepts 401 so that you don't get popups, and can instead deal with authentication errors in forms.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 1
    I'm using Forms Authentication, how can I cancel the redirect to login page and go to my custom page for error 401? I was looking for some related with HttpResponse.SuppressFormsAuthenticationRedirect but its only supported in .Net Framework 4.5 + – Lucas Konrath May 06 '14 at 02:50
0

I found the solution for my problem here

Using a custom authorization attribute for redirect to the "AccessDenied" page

Lucas Konrath
  • 567
  • 2
  • 8
  • 19
-1

Take a look at the solution for custom errors in MVC answered on another question within stackoverflow at https://stackoverflow.com/a/620559/686674

You want to override Application_Error rather than Application_EndRequest.

Community
  • 1
  • 1
Pepto
  • 1,434
  • 10
  • 13
  • ok.. i'll try it tomorrow. but seems to me an radical modification.. i would like to still using custom errors of ASP.NET – Lucas Konrath May 06 '14 at 02:54