简体   繁体   English

asp.net 403响应代码未触发自定义错误

[英]asp.net 403 response code not firing custom error

I have a custom error set in my web.config like so: 我在web.config中设置了一个自定义错误,如下所示:

<customErrors mode="On" defaultRedirect="Error.aspx">
    <error statusCode="403" redirect="AuthenticationRequired.aspx" />
    <error statusCode="404" redirect="NotFound.aspx" />
</customErrors>

I have some code on the page_load event that checks the users permissions... If this fails, I create the following response... but this is not caught by the custom error, anyone any ideas why? 我在page_load事件上有一些代码可以检查用户权限...如果失败,则创建以下响应...但这不是由自定义错误捕获的,任何人有任何想法吗?

Response.StatusCode = 403;
Response.Status = "403 Forbidden";
Response.End();

You should try to use the following code 您应该尝试使用以下代码

Response.TrySkipIisCustomErrors = true;

if you are using IIS 7. 如果您使用的是IIS 7。

Use Web.Server httpErrors instead: 改用Web.Server httpErrors

<system.webServer>
 <httpErrors errorMode="Custom" >
  <remove statusCode="404" subStatusCode="-1" />
  <error statusCode="404" path="/NotFound.aspx" responseMode="Redirect" />
 </httpErrors>
</system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM