PC Review


Reply
Thread Tools Rating: Thread Rating: 4 votes, 3.00 average.

How to catch error 401 access denied and redirect to custom error page ?

 
 
rote
Guest
Posts: n/a
 
      4th Aug 2008

I'm using ASP.NET 2.0 and i have copied and pasted the code below to my
Global.asax file but it desn't trap
the error
I want to trap the 401 access denied
void Application_Error(object sender, EventArgs e)
{


Exception exception = Server.GetLastError();

try

{

HttpException httpException = (HttpException)exception;

int httpCode = httpException.GetHttpCode();

switch (httpCode)

{

case 401: Response.Redirect("~/Pages/Error/NoAccess.aspx"); break;

case 404: Response.Redirect("~/Pages/Error/PageNotFound.aspx"); break;

default: Response.Redirect("~/Pages/Error/Generic.aspx"); break;

}

}

catch { }

Server.ClearError();

}
I know i can't use the normal custom error in the web config either beacuse
it doesn't work
Any ideas how to trap this?

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">


<error statusCode="401" redirect="accessdenied.htm" ></customErrors>
HTTP Error 401.1 - Unauthorized: Access is denied due to invalid
credentials.
Internet Information Services (IIS)






 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      4th Aug 2008
On Aug 4, 3:53*am, "rote" <naijaco...@hotmail.com> wrote:
> I'm using ASP.NET 2.0 and i have copied and pasted the code below to my
> Global.asax file but it desn't trap
> the error
> I want to trap the 401 access denied
> void Application_Error(object sender, EventArgs e)
> {
>
> Exception exception = Server.GetLastError();
>
> try
>
> {
>
> *HttpException httpException = (HttpException)exception;
>
> int httpCode = httpException.GetHttpCode();
>
> switch (httpCode)
>
> {
>
> *case 401: Response.Redirect("~/Pages/Error/NoAccess.aspx"); break;
>
> case 404: Response.Redirect("~/Pages/Error/PageNotFound.aspx"); break;
>
> default: Response.Redirect("~/Pages/Error/Generic.aspx"); break;
>
> *}
>
> }
>
> catch { }
>
> Server.ClearError();
>
> }
>
> I know i can't use the normal custom error in the web config either beacuse
> it doesn't work
> Any ideas how to trap this?
>
> <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
>
> <error statusCode="401" redirect="accessdenied.htm" ></customErrors>
> HTTP Error 401.1 - Unauthorized: Access is denied due to invalid
> credentials.
> Internet Information Services (IIS)


Look at the following tip
http://www.codeproject.com/KB/aspnet/Custon401Page.aspx

 
Reply With Quote
 
Patrick.O.Ige
Guest
Posts: n/a
 
      4th Aug 2008
Thanks Alexey.
But in asp.net 2.0 there is no Application_EndRequest event in the global
asax
and by the way i have to disable Allow ananonymous access..
Any ideas

"Alexey Smirnov" <(E-Mail Removed)> wrote in message
news:78aebe3f-a02e-4eaf-9343-(E-Mail Removed)...
On Aug 4, 3:53 am, "rote" <naijaco...@hotmail.com> wrote:
> I'm using ASP.NET 2.0 and i have copied and pasted the code belowe global
> .as to my
> Global.asax file but it desn't trap
> the error
> I want to trap the 401 access denied
> void Application_Error(object sender, EventArgs e)
> {
>
> Exception exception = Server.GetLastError();
>
> try
>
> {
>
> HttpException httpException = (HttpException)exception;
>
> int httpCode = httpException.GetHttpCode();
>
> switch (httpCode)
>
> {
>
> case 401: Response.Redirect("~/Pages/Error/NoAccess.aspx"); break;
>
> case 404: Response.Redirect("~/Pages/Error/PageNotFound.aspx"); break;
>
> default: Response.Redirect("~/Pages/Error/Generic.aspx"); break;
>
> }
>
> }
>
> catch { }
>
> Server.ClearError();
>
> }
>
> I know i can't use the normal custom error in the web config either
> beacuse
> it doesn't work
> Any ideas how to trap this?
>
> <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
>
> <error statusCode="401" redirect="accessdenied.htm" ></customErrors>
> HTTP Error 401.1 - Unauthorized: Access is denied due to invalid
> credentials.
> Internet Information Services (IIS)


Look at the following tip
http://www.codeproject.com/KB/aspnet/Custon401Page.aspx


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      4th Aug 2008
You'll need to convert this code to C#, but it works in VB.NET ...

In global.asax :

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Server.Transfer("Errors.aspx")
End Sub

Errors.aspx :
--------------------
<html>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim errMessage As String = ""
Dim appException As System.Exception = Server.GetLastError()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appException, HttpException)
Select Case checkException.GetHttpCode
Case 400
errMessage &= "Bad request. The file size is too large."
Case 401
errMessage &= "You are not authorized to view this page."
Case 403
errMessage &= "You are not allowed to view that page."
Case 404
errMessage &= "The page you have requested can't be found."
Case 408
errMessage &= "The request has timed out."
Case 500
errMessage &= "The server can't fulfill your request."
Case Else
errMessage &= "The server has experienced an error."
End Select
Else
errMessage &= "The following error occurred<BR>" & appException.ToString
End If
ErrorMessage.Text = errMessage & "<BR>We're sorry for the inconvenience."
Server.ClearError()
End Sub
</script>
<body>
<hr>
<asp:label id="ErrorMessage" font-size="12" font-bold="true" runat=server/>
<hr>
</body>
</html>
--------------

That traps 401, and the other listed errors, fine.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"rote" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
>
> I'm using ASP.NET 2.0 and i have copied and pasted the code below to my Global.asax file but it desn't trap
> the error
> I want to trap the 401 access denied
> void Application_Error(object sender, EventArgs e)
> {
>
>
> Exception exception = Server.GetLastError();
>
> try
>
> {
>
> HttpException httpException = (HttpException)exception;
>
> int httpCode = httpException.GetHttpCode();
>
> switch (httpCode)
>
> {
>
> case 401: Response.Redirect("~/Pages/Error/NoAccess.aspx"); break;
>
> case 404: Response.Redirect("~/Pages/Error/PageNotFound.aspx"); break;
>
> default: Response.Redirect("~/Pages/Error/Generic.aspx"); break;
>
> }
>
> }
>
> catch { }
>
> Server.ClearError();
>
> }
> I know i can't use the normal custom error in the web config either beacuse it doesn't work
> Any ideas how to trap this?
>
> <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
>
>
> <error statusCode="401" redirect="accessdenied.htm" ></customErrors>
> HTTP Error 401.1 - Unauthorized: Access is denied due to invalid credentials.
> Internet Information Services (IIS)
>
>
>
>
>
>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      4th Aug 2008
On Aug 4, 2:45*pm, "Patrick.O.Ige" <naijaco...@hotmail.com> wrote:
> Thanks Alexey.
> But in asp.net 2.0 there is no Application_EndRequest event in the global
> asax
> and by the way i have to disable Allow ananonymous access..
> Any ideas
>
> "Alexey Smirnov" <alexey.smir...@gmail.com> wrote in message
>
> news:78aebe3f-a02e-4eaf-9343-(E-Mail Removed)...
> On Aug 4, 3:53 am, "rote" <naijaco...@hotmail.com> wrote:
>
>
>
>
>
> > I'm using ASP.NET 2.0 and i have copied and pasted the code belowe global
> > .as to my
> > Global.asax file but it desn't trap
> > the error
> > I want to trap the 401 access denied
> > void Application_Error(object sender, EventArgs e)
> > {

>
> > Exception exception = Server.GetLastError();

>
> > try

>
> > {

>
> > HttpException httpException = (HttpException)exception;

>
> > int httpCode = httpException.GetHttpCode();

>
> > switch (httpCode)

>
> > {

>
> > case 401: Response.Redirect("~/Pages/Error/NoAccess.aspx"); break;

>
> > case 404: Response.Redirect("~/Pages/Error/PageNotFound.aspx"); break;

>
> > default: Response.Redirect("~/Pages/Error/Generic.aspx"); break;

>
> > }

>
> > }

>
> > catch { }

>
> > Server.ClearError();

>
> > }

>
> > I know i can't use the normal custom error in the web config either
> > beacuse
> > it doesn't work
> > Any ideas how to trap this?

>
> > <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

>
> > <error statusCode="401" redirect="accessdenied.htm" ></customErrors>
> > HTTP Error 401.1 - Unauthorized: Access is denied due to invalid
> > credentials.
> > Internet Information Services (IIS)

>
> Look at the following tiphttp://www.codeproject.com/KB/aspnet/Custon401Page.aspx- Hide quoted text -
>
> - Show quoted text -


Hi Patrick

you just need to add the code to you global.asax. It simply does not
add all methods by default... Moreover, if you may access the IIS, you
might try to use custom 401. Using the IIS Manager, go to the
properties of your site, then go to the Custom Errors tab to Edit the
various 401 errors and assign a custom redirection.

Hope this helps
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
App does not redirect to custom error page with ~(tilde) in URL =?Utf-8?B?QWxm?= Microsoft ASP .NET 4 14th Mar 2007 01:46 AM
Web.Config Timeout Expired Redirect Custom Error Page =?Utf-8?B?R3JhbnQ=?= Microsoft Dot NET 2 3rd Feb 2006 10:35 PM
custom error page for Statuscode 401.2 - Access Denied Rich Microsoft ASP .NET 1 26th May 2005 04:24 PM
redirect to my custom error page Mukund Patel Microsoft ASP .NET 1 24th Dec 2004 08:22 AM
Catch previous url in custom error page Jason Zhou Microsoft ASP .NET 1 24th Jun 2004 11:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:00 PM.