redirect to custom “access denied” page for ASP.NET Core

后端 未结 1 1656
不思量自难忘°
不思量自难忘° 2021-02-08 12:59

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

相关标签:
1条回答
  • 2021-02-08 13:39

    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}");
    
    0 讨论(0)
提交回复
热议问题