How to skip HTTP errors?

I use Response.Redirect to redirect the user to a new page, but I want the page to be displayed only if there is a specific parameter in the query line. Otherwise, I want to display the HTTP 401 authentication error page. Can this be done? If so, how? If not, why not?

+4
source share
3 answers

On your page, you can redirect to IHttpHandler , which will display the contents of your page with an authentication error and set the HTTP status code to 401.

 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Authentication error"); context.Response.StatusCode = 401; } 

The MSDN topic for IHttpHandler is some links that can help you understand what an HTTP handler is and how it can be implemented. The most important of them:

HTTP Handlers and HTTP Modules Overview

Walkthrough: Creating a Synchronous HTTP Handler

How to register HTTP handlers

+3
source

The following returns a 401 status code, you will need to add HTML to display itself depending on how IIS is configured.

  protected void Page_Load(object sender, EventArgs e) { if (String.IsNullOrEmpty(Request.QueryString["RedirectParam"])) Response.StatusCode = 401; else Response.Redirect("redirectpage.html"); } 
+2
source

I think I myself understood the answer itself, with some help from Joao and the answers of others.

I just need to write this:

  if (Request.Params[param] != nul) { ... } else { Response.ContentType = "text/html"; Response.Write("Authentication error"); Response.StatusCode = 401; Response.End(); } 

Simple! :) Let me know if there are any potential problems with this!

0
source

All Articles