Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect to custom "access denied" page for ASP.NET Core

I writing an ASP.NET Core 1.0 website that uses Windows Authentication. I have implemented Authorization and that is working as expected. Currently, when Authentication fails for a given user, a generic "HTTP 403" error page is shown.

enter image description here

How can I configure ASP.NET Core so that it redirects to my own custom "access denied" page?

I tried the approach outlined in this article, but it didn't work for me (maybe because I'm using Windows Auth instead of Forms Auth?) How to redirect unauthorized users with ASP.NET MVC 6

Any help would be appreciated.

like image 881
Tory E Avatar asked Jul 20 '16 22:07

Tory E


1 Answers

Use the status code pages middleware.

app.UseStatusCodePages(async context => {
  if (context.HttpContext.Response.StatusCode == 403)
  {
     // your redirect
  }
});

You can also choose more generic approach via app.UseStatusCodePagesWithRedirects. The middleware can handle redirects (with either relative or absolute URL paths), passing the status code as part of the URL. For example the following will redirect to ~/errors/403 for 403 error:

app.UseStatusCodePagesWithRedirects("~/errors/{0}");
like image 135
Set Avatar answered Oct 13 '22 18:10

Set