Wednesday, April 22, 2009

Programatically sending 403 Forbidden error

Scenario:
I wanted to programtically send 403 Forbidden error if certain condition is not while making a page request.

Solution:
I tested my ASP.Net basics.

Code:

internal static void Send403(HttpResponse response)
{
    SendResponse(response, 0x193, "403 FORBIDDEN");
}

internal static void SendResponse(HttpResponse response, int code, string strBody)
{
     HttpContext current = HttpContext.Current;
     object obj2 = current.Items["ResponseEnded"];
     if ((obj2 == null) || !((bool)obj2))
     {
        current.Items["ResponseEnded"] = true;
        response.StatusCode = code;
        response.Clear();
        if (strBody != null)
        {
            response.Write(strBody);
        }
        response.End();
      }
}

0 comments: