2

I have a try catch block to handle an error I am getting with my application. I would like a simple way of setting the response to status code 403 or forbidden and then either redirect the user to the login page or to a custom error page.

I am having some issue with once setting the status code and the redirect. Anyone have an example of setting the status code and then redirecting?

lispmachine
  • 4,407
  • 1
  • 22
  • 31
JPJedi
  • 1,498
  • 7
  • 32
  • 58

2 Answers2

6
Response.Status = "403 Forbidden";
Response.Addheader("Location", "http://stackoverflow.com/");

This in C#, but the concept should be pretty much the same in most languages.

Brandon
  • 68,708
  • 30
  • 194
  • 223
  • I get the following when I try to run it locally: This operation requires IIS integrated pipeline mode. – JPJedi Mar 24 '09 at 17:54
  • Are you using Response.Headers.Add or Response.AddHeader? You can also try changing it to Context.Response.AddHeader. – Brandon Mar 24 '09 at 17:57
  • Should I clear of close the request when it redirects to the login page? – JPJedi Mar 24 '09 at 18:35
  • I don't believe you need to, it should handle it automatically. – Brandon Mar 24 '09 at 18:38
  • The issue now is. In the catch block I am redirecting but the code continues to run outside the catch block. So I am trying to stop/clear the response once I redirect it. I hope that makes sense. Thanks a bunch for the help. – JPJedi Mar 24 '09 at 18:41
  • If you don't want the code to run, why not put a return statement after you set the header? – Brandon Mar 24 '09 at 19:14
1

Duplicate: What should the HTTP response be when the resource is forbidden but there’s an alternate resource?

Sending Location header is only intended for 3xx (redirect) or 201 Created responses. Although it may work with most of the clients it's IMHO not how HTTP was designed.

If you really care about correct status codes and following HTTP specification why don't you respond with 303 See Other or use HTTP authentication.

Community
  • 1
  • 1
lispmachine
  • 4,407
  • 1
  • 22
  • 31