本文介绍了重定向到自定义“访问被拒绝" ASP.NET Core的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我编写了一个使用Windows身份验证的ASP.NET Core 1.0网站.我已经实现了授权,并且按预期方式工作.当前,当给定用户的身份验证失败时,将显示一般的"HTTP 403"错误页面.

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.

如何配置ASP.NET Core,使其重定向到我自己的自定义访问被拒绝"页面?

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

我尝试了本文中概述的方法,但是它对我不起作用(也许是因为我使用的是Windows Auth而不是Forms Auth?)如何使用ASP.NET MVC 6重定向未经授权的用户

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

任何帮助将不胜感激.

推荐答案

使用状态代码页中间件.

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

您还可以通过app.UseStatusCodePagesWithRedirects选择更通用的方法.中间件可以处理重定向(使用相对或绝对URL路径),并将状态代码作为URL的一部分传递.例如,以下内容将重定向到~/errors/403,以显示403错误:

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}");

这篇关于重定向到自定义“访问被拒绝" ASP.NET Core的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:13