Search code examples
c#asp.netunauthorizedaccessexcepti

How to change where UnauthorizedAccessException redirects to in ASP.Net


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?


Solution

  • You can use multiple exception handlers

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