Sab*_*wan 16 unauthorized http-status-code-403 c#-4.0 asp.net-mvc-3
我重写该类以执行自定义授权
[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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在在web.config中我配置了403错误页面
<customErrors defaultRedirect="/Shared/Error" mode="On">
<error statusCode="403" redirect="/Shared/UnAuthorize" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
但是浏览器仍然显示403的默认错误页面,我在这里缺少任何想法
Kev*_*oet 11
除了Max B之外,只是一个小小的提示/注释:
当我使用自定义错误时,我会创建一个ErrorsController和UnAuthorize ActionResult并执行以下操作:
<error statusCode="403" redirect="/Errors/UnAuthorize" />
Run Code Online (Sandbox Code Playgroud)
这样我可以在控制器中添加额外信息或执行其他操作,例如:
通过这种方式,您可以更好地控制正在发生的事情.
gen*_*i98 11
我知道这是一个非常古老的问题,但我发帖给可能有同样问题的人.像我一样,我有同样的问题并解决了它.如果要在web.config中触发customErrors元素,可以在下面尝试.
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
throw new HttpException(403, "Forbidden");
}
Run Code Online (Sandbox Code Playgroud)
我遇到了与编写自己的自定义AuthorizeAttribute时完全相同的问题.当我在web.config中添加"customErrors"标记时,403的自定义错误页面将不会显示.这就是我解决的方法:
[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 RedirectToRouteResult(
new RouteValueDictionary(
new
{
controller = "Error",
action = "Unauthorised"
})
);
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
Run Code Online (Sandbox Code Playgroud)
分配了一个我想要显示给filterContext.Result的路由,而不是分配403 HttpStatusCode.
| 归档时间: |
|
| 查看次数: |
22005 次 |
| 最近记录: |