简体   繁体   中英

asp.net 403 response code not firing custom error

I have a custom error set in my web.config like so:

<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?

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.

Use Web.Server httpErrors instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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