重定向错误 401.2 未经授权

sd_*_*ula 5 c# asp.net redirect web-config

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

<customErrors defaultRedirect="Error.aspx" mode="On">
      <error statusCode="401" redirect="Unauthorized.aspx" />
    </customErrors>
Run Code Online (Sandbox Code Playgroud)

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

sd_*_*ula 6

将以下内容添加到 global.asax 似乎工作得很好:

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


Kap*_*wal 0

首先尝试这个:

在 Unauthorized.aspx.cs Page_Load Method 上设置:

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

详细请看这个:

HttpResponse.TrySkipIisCustomErrors 属性

IIS 7 错误页面出现超过 500 个错误

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

<system.webServer>
      <httpErrors existingResponse="PassThrough" />
</system.webServer>
Run Code Online (Sandbox Code Playgroud)