Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set 403 error page in MVC

I overrides the class to perform custom Authorization

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAuthenticated)
        {
            filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(403);
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}

now in web.config i have configured the 403 error page

<customErrors defaultRedirect="/Shared/Error" mode="On">
  <error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>

but the browser still shows me default error page for 403, what i am missing here, any idea

like image 384
Saboor Awan Avatar asked Aug 11 '11 07:08

Saboor Awan


People also ask

How do I trigger a 403 error?

The most common cause of a 403 Forbidden Error is simply inputting an incorrect URL. As discussed before, many tightly secured web servers disallow access to improper URLs. This could be anything from accessing a file directory to accessing a private page meant for other users.

Can you fix a 403 Forbidden error?

Fortunately, you can fix this problem in an easy way — just create a new server configuration file. Other possible causes of a 403 forbidden error include: Incorrect IP address: A domain name directs to an incorrect or outdated IP address hosting a site that prevents you from gaining access.


1 Answers

Just a small hint/note besides Max B. answer:

When I'm using custom errors I make an ErrorsController, and a UnAuthorize ActionResult and do the following:

<error statusCode="403" redirect="/Errors/UnAuthorize" />

This way I can add extra information or do other actions in my controller, for example:

  • Like logging to the database that someone tried to access an authenticated area.
  • Error counting.
  • Maybe a bug or report form that they can use to send the admin information.
  • ...

This way you have some more control on what's happening.

like image 162
Kevin Cloet Avatar answered Oct 23 '22 22:10

Kevin Cloet