【问题标题】:Azure portal. Mvc. Redirect to custom error page doesn't work for 403 errorAzure 门户。 MV。重定向到自定义错误页面不适用于 403 错误
【发布时间】:2018-04-01 19:49:08
【问题描述】:

我在MVC 应用程序中配置了owin role-based 授权。 然后,我需要添加403http错误的自定义处理

我知道两种方法:

  1. Web.config 设置:

    <customErrors mode="Off" defaultRedirect="~/Error/" redirectMode="ResponseRedirect">
     <error statusCode="403" redirect="~/Error/NoAccess" />
    </customErrors>
    
  2. Authorize 属性中覆盖的 HandleUnauthorizedRequest 方法内部的配置:

    if (filterContext.HttpContext.User?.Identity.IsAuthenticated ?? false)
    {
       filterContext.Result = new RedirectToRouteResult(
       new RouteValueDictionary
       {
         {"action", "NoAccess"},
         {"controller", "Error"}
       });
       //I've tried two variants of below: with below line as well as without it 
       filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
    }
    

当我尝试访问我的用户不允许的资源并且我看到403 错误的自定义页面时,这两种方法在我的本地计算机上运行良好,但是当我在azure 上部署我的应用程序时门户网站,我只看到带有以下文本的白页:'您无权查看此目录或页面。'。据我所知,我需要在 azure 门户上配置此行为,并在我的代码中对其进行配置。

有人可以建议吗?

【问题讨论】:

    标签: owin http-status-code-403 authorize-attribute azureportal role-base-authorization


    【解决方案1】:

    我找到了答案。 如果我在控制器方法中手动设置响应代码,则会出现问题,如下所示:

    public ActionResult NoAccess()
    {
        Response.StatusCode = 403;
        return View();
    }
    

    如果我删除此状态设置,重定向工作正常。 解决方案是将以下标志设置为 true:TrySkipIisCustomErrors

     public ActionResult NoAccess()
     {
         Response.TrySkipIisCustomErrors = true;
         Response.StatusCode = 403;
    
         return View();
     }
    

    然后一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多