473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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


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)


Aug 4 '08 #1
5 24477
On Aug 4, 3:53*am, "rote" <naijaco...@hotmail.comwrote:
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

Aug 4 '08 #2
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" <al************@gmail.comwrote in message
news:78**********************************@d77g2000 hsb.googlegroups.com...
On Aug 4, 3:53 am, "rote" <naijaco...@hotmail.comwrote:
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
Aug 4 '08 #3
"Patrick.O.Ige" <na********@hotmail.comwrote in message
news:eh**************@TK2MSFTNGP03.phx.gbl...
But in asp.net 2.0 there is no Application_EndRequest event in the global
asax
???

http://msdn.microsoft.com/en-us/libr...st(VS.80).aspx

protected void Application_EndRequest (Object sender, EventArgs e)
{

}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 4 '08 #4
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" <na********@hotmail.comwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
>
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)


Aug 4 '08 #5
On Aug 4, 2:45*pm, "Patrick.O.Ige" <naijaco...@hotmail.comwrote:
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.comwrote in message

news:78**********************************@d77g2000 hsb.googlegroups.com...
On Aug 4, 3:53 am, "rote" <naijaco...@hotmail.comwrote:


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
Aug 4 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrew Alger | last post by:
Ok here is the story. Test Environment: 1 IIS Box Win 2k 1 SQL Box Win 2k Live Environment: 1 IIS Box Win 2k3 1 SQL Box Win 2k
1
by: Grant | last post by:
Hi, I have searched the net and still no luck... I just want to automatically have a page redirected to another page when the timeout set in the config file expires. I currently have: ...
1
by: Lyn Duong | last post by:
Hi, I have tried creating a linked server in microsoft sql 2000 to a db2 for vm database. I use a db2 v8 admin client, configure it via client configuration setup and then in Microsoft SQL...
1
by: Michael Hetrick | last post by:
If I set authorization in a web.config file to only allow certain individuals access to pages, those users who are not named receive an access denied message: ---------------------------------...
5
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times...
7
by: Martin Strojek | last post by:
Hi, I have the following problem with developing some web site. I use Visual Studio 2003 to build a website. I tried Windows 2003 Server and switched now back to Windows XP with PWS but the...
3
by: markus.rietzler | last post by:
i want to do (multiple) file upload(s) and display a progress bar. with firefox and safari it is no problem at all. only IE makes some problems. my script is based on ajax-uploader, which can be...
4
by: =?Utf-8?B?QWxm?= | last post by:
Hello all, I am having trouble dealing with ~(tilde) in my .Net 1.1 web application, specially when it comes through the URL. For example, when someone requests the following URL:...
4
bhing
by: bhing | last post by:
please help me.. i dont really understand whats happening in my server.... i have a website using php... when i try to access it this is the message.... Fatal error: Access denied for user...
1
by: Robert Bustos | last post by:
Hello everyone, I am a newbie here and I need some help. I am trying to install a bug tracking application called MantisBT on a Windows Server 2008 machine. It requires a Web Server - running...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.