Question

Pretty straightforward.
I'm throwing an UnauthorizedAccessException in an AuthorizationFilter. I want UnauthorizedAccessException to head to an Error page, NOT to the /Account/Login page.
How can I make that change?

Was it helpful?

Solution

You can use multiple exception handlers

        try
        {
            // code here
        }
        catch (UnauthorizedAccessException)
        {
            Response.Redirect(errorPageUrl, false);
        }
        catch (Exception)
        {
            Response.Redirect(loginPageUrl, false);
        }

OTHER TIPS

Try setting up something like this in global.asax.cs

protected void Application_Error(object sender, EventArgs e)
{
    // excpt is the exception thrown
    // The exception that really happened is retrieved through the GetBaseException() method
    // because the exception returned by the GetLastError() is a HttpException
    Exception excpt = Server.GetLastError().GetBaseException();

      if(excpt is UnauthorizedAccessException)
      { 
        // redirect here
      }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top