tags:

views:

819

answers:

3

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?

+4  A: 
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
I get the following when I try to run it locally:This operation requires IIS integrated pipeline mode.
JPJedi
Are you using Response.Headers.Add or Response.AddHeader? You can also try changing it to Context.Response.AddHeader.
Brandon
Should I clear of close the request when it redirects to the login page?
JPJedi
I don't believe you need to, it should handle it automatically.
Brandon
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
If you don't want the code to run, why not put a return statement after you set the header?
Brandon
A: 

I am going to give credit to Brandon on this. Because this way did work but during this little exercise we decided to make the resulting answer/solution a little easier and just created a simple page that we redirected to when we needed to. We did not worry about the setting of the status because we really didn't need to do it.

But Thank You Brandon for your 2 cents though.

JPJedi
+1  A: 

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.

lispmachine