【问题标题】:redirecting on error 401.2 unauthorized重定向错误 401.2 未经授权
【发布时间】:2012-06-10 14:22:20
【问题描述】:

我一直在尝试重定向错误消息 401.2。:未经授权:由于服务器配置,登录失败,如下所示:

<customErrors defaultRedirect="Error.aspx" mode="On">
      <error statusCode="401" redirect="Unauthorized.aspx" />
    </customErrors>

但它实际上从未重定向。它仍然显示默认的访问被拒绝页面。 我在那里做错了什么?

【问题讨论】:

    标签: c# asp.net redirect web-config


    【解决方案1】:

    将以下内容添加到 global.asax 似乎工作正常:

    void Application_EndRequest(object sender, EventArgs e)
            {
                if (Response.StatusCode == 401)
                    Response.Redirect("Unauthorized.aspx");
            }
    

    【讨论】:

      【解决方案2】:

      先试试这个:

      在你的 Unauthorized.aspx.cs Page_Load 方法上设置:

      private void Page_Load(System.Object sender, System.EventArgs e)
      {
          //Put user code to initialize the page here 
          Response.StatusCode = 401;
          Response.TrySkipIisCustomErrors = true;
      }
      

      查看详情:

      HttpResponse.TrySkipIisCustomErrors Property

      IIS 7 Error Pages taking over 500 Errors

      第二次试试这个: 除了标记之外,还可以使用它来绕过 IIS 错误页面:

      <system.webServer>
            <httpErrors existingResponse="PassThrough" />
      </system.webServer>
      

      【讨论】:

      • 没有变化。有一种方法可以在 global.asax 中拦截 EndRequest,但不知道该怎么做。我发现的只有例子是在 VB 中
      • 我已经尝试了上述两个步骤。仍然没有开火,只是去 YSOD。它永远不会到达 Unauthorized.aspx 页面,甚至看起来也不会到达 global.asax
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 2016-04-18
      • 2021-07-22
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多