【问题标题】:Why does my mvc project redirect to default 403 error page instead of the one I have redirected it to, on IIS not on local?为什么我的 mvc 项目重定向到默认的 403 错误页面,而不是我重定向到的页面,而不是在本地的 IIS 上?
【发布时间】:2018-11-18 04:04:43
【问题描述】:

我已经设置了一个错误页面,如果抛出 403 错误并且它可以在本地环境中工作,但在部署到 IIS 时它会重定向到默认的 403 禁止页面,即 403 - 禁止访问:拒绝访问。

p>

为什么?

代码:

web.config:

<system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
    <globalization uiCulture="en" culture="en-GB"/>
    <customErrors mode="Off"></customErrors>
    <authentication mode="Forms"></authentication>
  <sessionState timeout="60"></sessionState>
  </system.web> 

C#:

自定义授权:

 public class CustomeAuthorize: AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            // just to ensure if the unauthorized request is from an authenticated user or a visitor.

            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)   // just a visitor since he/she doesn't need any login to proceed
            {
                //filterContext.Result = new ViewResult { ViewName = "~/Views/Errors/AuthorizeFailedError.cshtml" };  
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Errors", action = "AuthorizeFailedError" }));
            }
            else //authenticated user but authorized to the requested page
            {
                filterContext.Result = new ViewResult { ViewName = "~/Views/Login/Login.cshtml" };  
            }

        }

【问题讨论】:

    标签: c# asp.net-mvc web http-status-code-403


    【解决方案1】:

    由于我是新手,并且看到我的答案没有很好地显示出来,我会再试一次。

    我看到您的 web.config 中的 customErrors 没有任何重定向,因为 IIS 将使用此尝试添加类似这样的内容

    <customErrors mode="Off">
      <error redirect="~/Error/Error403Page" statusCode="403" />
    </customErrors>
    

    或者如果你有一个默认的错误页面,你可以这样做

    <customErrors mode="Off" defaultRedirect="~/Error">
      <error redirect="~/Error/Error403Page" statusCode="403" />
    </customErrors>
    

    更多信息可以在这里找到 https://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 2014-05-15
      相关资源
      最近更新 更多