在MVC中设置403错误页面
我现在在 web.config 中重写该类以执行自定义授权
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(403);
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
,我已经配置了 403 错误页面
<customErrors defaultRedirect="/Shared/Error" mode="On">
<error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>
,但浏览器仍然显示 403 的默认错误页面, 我在这里缺少什么,任何想法
I overrides the class to perform custom Authorization
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(403);
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
now in web.config i have configured the 403 error page
<customErrors defaultRedirect="/Shared/Error" mode="On">
<error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>
but the browser still shows me default error page for 403,
what i am missing here, any idea
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我知道这是一个非常老的问题,但我是为可能有同样问题的人发帖。像我一样,我也遇到了同样的问题并解决了。如果你想触发web.config中的customErrors元素,你可以尝试下面的方法。
I know this is very old question, but I'm posting for someone who might have same issue. Like me, I had same issue and solved it. If you want to trigger the customErrors element in web.config, you can try below.
除了 Max B 之外,还有一个小提示/注释:
当我使用自定义错误时,我会创建一个 ErrorsController 和 UnAuthorize ActionResult 并执行以下操作:
这样我可以添加额外的信息或执行其他操作例如,我的控制器中的操作:
这样您就可以更好地控制正在发生的事情。
Just a small hint/note besides Max B. answer:
When I'm using custom errors I make an
ErrorsController
, and a UnAuthorize ActionResult and do the following:This way I can add extra information or do other actions in my controller, for example:
This way you have some more control on what's happening.
当我编写自己的自定义 AuthorizeAttribute 时,我遇到了与您完全相同的问题。当我在 web.config 中添加“customErrors”标签时,403 的自定义错误页面不会显示。
这就是我解决这个问题的方法:
分配一个我想要显示的路由到filterContext.Result,而不是分配403 HttpStatusCode。
I had the exact same issue as you had when i wrote my own custom AuthorizeAttribute. The custom errors page for 403 won't show up when I added "customErrors" tag in web.config.
This is how I got it resolved:
Assigned a route that I would like to display to filterContext.Result, instead of assigning 403 HttpStatusCode.
或者您可以执行此替代解决方案,而不是使用:
您可以将其更改为:
并在您的 Controller/BaseController 中重写方法 OnException(ExceptionContext filterContext)
Or you can do this alternative solution,instead of using :
you can change it to :
And override method OnException(ExceptionContext filterContext) in your Controller/BaseController
对我来说, HttpStatusCodeResult(403) 似乎位于错误的 if 分支中。在我看来,代码应该如下所示:
To me it seems that the the HttpStatusCodeResult(403) is in the wrong if branch. In my opinion the code should look like this:
如何在mvc中处理401(未经授权)、403(禁止)和500(内部服务器错误)。用于 ajax/非 ajax 调用和 aspx 表单身份验证。
可以对其进行更改,以便以不同的方式处理各种未捕获的异常,并且无论请求是否为 ajax,都可以做出不同的反应。 auth 部分允许它绕过任何常规的 mvc Web 表单重定向到登录页面,而是返回未经授权的 401 - 然后您的客户端 js 框架可以更轻松地对 http 状态 401/403 做出反应。
Web.config:
Web 服务 api 请求的附加路由:(放在常规 mvc 路由之上)
jquery 处理错误的示例客户端代码:
或者,您可以使所有身份验证都通过 ApplicationHandleErrorAttribute,并摆脱 web.config 拒绝用户=“?”。但我有一个遗留的 aspx 页面,它没有达到 mvc 过滤,所以我希望拒绝 users="?"。
How to handle 401 (Unauthorized), 403 (Forbidden) and 500 (Internal Server Error) in mvc. For ajax/non-ajax calls and under aspx forms authentication.
It can be altered to handle various uncaught exceptions differently and react differently whether the request is ajax or not. The auth part allows it to bypass any regular mvc web forms redirect-to-login-page and instead return 401 unauthorized - then your client-side js framework can react to http status 401/403 more easily.
Web.config:
Additional route for web service api requests: (put above regular mvc route)
Sample client side code for jquery to handle the error:
Alternatively, you could make all auth go through ApplicationHandleErrorAttribute, and get rid of that web.config deny users="?". But I have a legacy aspx page which doesn't hit the mvc filtering so I want that deny users="?".
1-创建一个类调用
LoggedOrAuthorizedAttribute
2-将您创建的属性添加到操作的顶部
1-create a class call
LoggedOrAuthorizedAttribute
2-Add the attribute you created to the top of your actions