views:

22

answers:

2

The HttpForbiddenHandler Class is sealed however I'd like to create a class that behaves like it. Something like this:

public class ForbiddenHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // do the 403 here somehow
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

How would I cause that 403 redirect?

+1  A: 
context.Response.StatusCode = 403;
context.Response.Redirect("403.aspx");
Darin Dimitrov
Wouldn't the second line change the status code to 302?
Jørn Schou-Rode
+2  A: 

If you simply want to send the 403 status code:

context.Response.Status = "403 Forbidden";

Also, you might want to write some message to the client:

context.Response.Write("This is very much forbidden!");

If you wish to redirect the user to the 403 custom error page, configured in your web.config or machine.config, you should be able to do it like this:

throw new HttpException(403, "Forbidden");
Jørn Schou-Rode