Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to explicitly raise an HTTP error?

Posted on 2004-04-27
10
Medium Priority
?
1,747 Views
Last Modified: 2008-02-01
Hi,

I'm currently developping with ASP.NET using C# and I wanted to know if it's possible to raise an HTTP error explicitly. I want, for example, a forbidden 403 error to be displayed when the user tries to access certain pages or click on certain links. I would be grateful if someone could help. Thanks...

Ash.

0
Comment
Question by:ashv27
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:b_smith_79
ID: 10936008
Wow, you are the first person I have met who actually wanted their application to throw the default error messages :)

I would just redirect the user to a page that all users are explicitly denied access to if I had a real need to only ever use the IIS error page.
0
 

Author Comment

by:ashv27
ID: 10936326
Thanks for your reply. Is there any other way of achieving this without directly redirecting the user to the error page. I'm repeating myself, but is it possible to raise the error and let IIS do the redirection?
0
 
LVL 2

Expert Comment

by:b_smith_79
ID: 10936373
I don't know of a line of code that you can write to just throw your choice of error but it would be useful.

IIS does a server.transfer to whatever page is configured as the error page, so just emulating its behaviour is another option.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10936405
you can do that with server side language scripting....
In php...

<?php
if ((!isset($PHP_AUTH_USER)) ||
    (!isset($PHP_AUTH_PW)) ||
    ($PHP_AUTH_USER != "guest") ||
    ($PHP_AUTH_PW != "guest"))
{
    header('WWW-Authenticate: Basic realm="Private Area"');
    header("HTTP/1.1 401 Unauthorized");
    print "This page requires authorisation.";
    exit();
}
else
{
    print "You're through to the secret page, was the effort worth it?";
}
?>

0
 
LVL 2

Expert Comment

by:b_smith_79
ID: 10936445
Why do you want to do this, by the way?

If it's your intention to actually trip the internal access denied routine in IIS for logging purposes or whatever, then neither my nor the above solution will really work.
0
 

Author Comment

by:ashv27
ID: 10936661
The reason is that I have several pages which take their parameters from their respective url by the GET method (query string) and would like to thow a 403 forbidden error if the pages are called without the proper parameters in the url.
0
 
LVL 2

Expert Comment

by:SatanUK
ID: 10937570
well the ASP.NET Version of the PHP Header is

Response.AddHeader(name,value)

but im not sure if "HTTP/1.1 401 Unauthorized" is the name or value
0
 

Expert Comment

by:crimsonIE
ID: 10939515
give this a go:

void Page_Load(Object src, EventArgs args) {
   // raise an intentional exception to get the ball rolling
   throw new ApplicationException("This is an unhandled exception.");
}

void Page_Error(Object sender, EventArgs args) {
   Response.Write("Error:\n");
   Exception e = Server.GetLastError();
   Trace.Write("Message",e.Message);
   Trace.Write("Source",e.Source);
   Trace.Write("Stack Trace",e.StackTrace);
Response.Write("Sorry, an error was encountered.");
   Context.ClearError();
}
0
 

Accepted Solution

by:
crimsonIE earned 250 total points
ID: 10939639
This is actually a lot simpler. Edit you web.config file and enter the following.

1) If you want a generic page for ANY error then:

   <customErrors mode="remoteonly" defaultRedirect="error.htm">

2) If you want specific error messages for specific errors like 404, 500, 501 then
    (add in errror statuscode line for each one. in this case i have one in for the 404 error)

<customErrors mode="remoteonly" defaultRedirect="error.htm">
  <error statusCode="404" redirect="pagenotfound.htm"/>
</customErrors>


List of status codes:
HTTP Status Codes

100 - Continue
101 - Switching Protocols
200 - OK
201 - Created
202 - Accepted
203 - Non-Authoritative Information
204 - No Content
205 - Reset Content
206 - Partial Content
300 - Multiple Choices
301 - Moved Permanently
302 - Found
303 - See Other
304 - Not Modified
305 - Use Proxy
306 - No Longer Used
307 - Temporary Redirect
400 - Bad Request
401 - Not Authorised
402 - Payment Required
403 - Forbidden
404 - Not Found
405 - Method Not Allowed
406 - Not Acceptable
407 - Proxy Authentication Required
408 - Request Timeout
409 - Conflict
410 - Gone
411 - Length Required
412 - Precondition Failed
413 - Request Entity Too Large
414 - Request URI Too Long
415 - Unsupported Media Type
416 - Requested Range Not Satisfiable
417 - Expectation Failed
500 - Internal Server Error
501 - Not Implemented
502 - Bad Gateway
503 - Service Unavailable
504 - Gateway Timeout
505 - HTTP Version Not Supported
0
 

Author Comment

by:ashv27
ID: 10942264
Hi,

Thanks for your posts. venkateshwarr's approach sounded promising; i even tried it using PHP, but it only echoes the message following the header without any server redirection to the appropriate error page.

crimsonIE your solution shows what to do once the error is thrown but not how to explicitly throw the error. Anyway i think that i'll make my own custom error pages as you suggested, and do the redirection myself when the need arises.

Thanks again...
 
0

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question