473,763 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Err or(object sender, EventArgs e)
{
Exception exception = Server.GetLastE rror();

try

{

HttpException httpException = (HttpException) exception;

int httpCode = httpException.G etHttpCode();

switch (httpCode)

{

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

case 404: Response.Redire ct("~/Pages/Error/PageNotFound.as px"); break;

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

}

}

catch { }

Server.ClearErr or();

}
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="RemoteOnl y" defaultRedirect ="GenericErrorP age.htm">
<error statusCode="401 " redirect="acces sdenied.htm" ></customErrors>
HTTP Error 401.1 - Unauthorized: Access is denied due to invalid
credentials.
Internet Information Services (IIS)


Aug 4 '08 #1
5 24520
On Aug 4, 3:53*am, "rote" <naijaco...@hot mail.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_Err or(object sender, EventArgs e)
{

Exception exception = Server.GetLastE rror();

try

{

*HttpException httpException = (HttpException) exception;

int httpCode = httpException.G etHttpCode();

switch (httpCode)

{

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

case 404: Response.Redire ct("~/Pages/Error/PageNotFound.as px"); break;

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

*}

}

catch { }

Server.ClearErr or();

}

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="RemoteOnl y" defaultRedirect ="GenericErrorP age.htm">

<error statusCode="401 " redirect="acces sdenied.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_End Request 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******** *************** ***********@d77 g2000hsb.google groups.com...
On Aug 4, 3:53 am, "rote" <naijaco...@hot mail.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_Err or(object sender, EventArgs e)
{

Exception exception = Server.GetLastE rror();

try

{

HttpException httpException = (HttpException) exception;

int httpCode = httpException.G etHttpCode();

switch (httpCode)

{

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

case 404: Response.Redire ct("~/Pages/Error/PageNotFound.as px"); break;

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

}

}

catch { }

Server.ClearErr or();

}

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="RemoteOnl y" defaultRedirect ="GenericErrorP age.htm">

<error statusCode="401 " redirect="acces sdenied.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********@hot mail.comwrote in message
news:eh******** ******@TK2MSFTN GP03.phx.gbl...
But in asp.net 2.0 there is no Application_End Request event in the global
asax
???

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

protected void Application_End Request (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_Err or(ByVal sender As Object, ByVal e As EventArgs)
Server.Transfer ("Errors.asp x")
End Sub

Errors.aspx :
--------------------
<html>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim errMessage As String = ""
Dim appException As System.Exceptio n = Server.GetLastE rror()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appExcept ion, 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.To String
End If
ErrorMessage.Te xt = errMessage & "<BR>We're sorry for the inconvenience."
Server.ClearErr or()
End Sub
</script>
<body>
<hr>
<asp:label id="ErrorMessag e" 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********@hot mail.comwrote in message news:%2******** ********@TK2MSF TNGP05.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_Err or(object sender, EventArgs e)
{
Exception exception = Server.GetLastE rror();

try

{

HttpException httpException = (HttpException) exception;

int httpCode = httpException.G etHttpCode();

switch (httpCode)

{

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

case 404: Response.Redire ct("~/Pages/Error/PageNotFound.as px"); break;

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

}

}

catch { }

Server.ClearErr or();

}
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="RemoteOnl y" defaultRedirect ="GenericErrorP age.htm">
<error statusCode="401 " redirect="acces sdenied.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...@hot mail.comwrote:
Thanks Alexey.
But in asp.net 2.0 there is no Application_End Request 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******** *************** ***********@d77 g2000hsb.google groups.com...
On Aug 4, 3:53 am, "rote" <naijaco...@hot mail.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_Err or(object sender, EventArgs e)
{
Exception exception = Server.GetLastE rror();
try
{
HttpException httpException = (HttpException) exception;
int httpCode = httpException.G etHttpCode();
switch (httpCode)
{
case 401: Response.Redire ct("~/Pages/Error/NoAccess.aspx") ; break;
case 404: Response.Redire ct("~/Pages/Error/PageNotFound.as px"); break;
default: Response.Redire ct("~/Pages/Error/Generic.aspx"); break;
}
}
catch { }
Server.ClearErr or();
}
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="RemoteOnl y" defaultRedirect ="GenericErrorP age.htm">
<error statusCode="401 " redirect="acces sdenied.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.a spx- 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
1322
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
598
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: <sessionState
1
4100
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 Server 2000 enterprise manager, I create a linked server, I use ibm oledb driver for db2, datasource = mydatabase, product=my database, provider = db2oledb. When I try to access the database via an openquery to the linked server, I get an error from...
1
1668
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: --------------------------------- Access is denied. Description: An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL.
5
2951
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 I try to start to run/debug a project (through Visual Studio) Server Error in '/Intranet' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during...
7
4778
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 error occurs in both installations. When I do a compile/build of my app and go to the browser and hit reload there comes an error:
3
19882
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 found at www.srmiles.com/freestuff/ajax_file_uploader/ . you can do multiple file uploads. each upload will have it's own "form"-tag, so that each file is uploaded for its own. could be a good solution if there are "big" uploads.
4
4451
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: www.mysite.com/~mypage.aspx, my web application will through the following error (with custom errors turned off): Server Error in '/' Application. --------------------------------------------------------------------------------
4
3957
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 'apache'@'localhost' (using password: YES) why is that so? when in fact i dont have any apache user on my mysql.user, please.. i really need the answer asap.. tnx
1
2982
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 Apache 2.2, PHP 5, and Mysql 5.5. Apache is running and I have verified that PHP and MySQL are communicating with one another. When trying to run the actual MantisBT application I get the following error message: APPLICATION ERROR #401 ...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.