You can handle the 401 in EndRequest if the 401 was generated by the
UrlAuthorizationModule. You can't if it was handled by IIS as the request
will never get to ASP.NET.
All the UrlAuthorizationModule does is checks the access of the user and if
it fails, sets status to 401 and calls CompleteRequest which short circuits
the event pipeline to the EndRequest method. This is nice because if it had
called Response.End, the response would have been aborted right there.
However, the UrlAuthorizationModule does its work politely and gives you one
last chance at the request.
Here is some VB.NET code that I modified from an actual thing we wrote that
does something similar. I didn't test this version, but you get the idea.
This would basically be the gut of an IHttpModule, although you could do
something similar in Global.asax:
Public Sub Init(ByVal context As System.Web.HttpApplication) _
Implements System.Web.IHttpModule.Init
AddHandler context.EndRequest, AddressOf HandleComplete
End Sub
Private Sub HandleComplete(ByVal sender As Object, ByVal e As EventArgs)
Dim application As HttpApplication
application = DirectCast(sender, HttpApplication)
If application.Context.Response.StatusCode = 401 Then
application.Context.Response.StatusCode = 200
application.Context.Response.Clear()
application.Context.Response.Write("Access Denied")
application.Context.Response.Flush()
End If
End Sub
In your code you might wish to redirect or at least render some valid HTML

, but you get the idea.
HTH,
Joe K.
"jfer" <> wrote in message
news: oups.com...
> Interesting advice Joe thank you very much. Do you have any articles
> on this subject to help me begin an implementation?
> Also are you SURE you can trap a 401* error? I could of swore I read
> others trying to trap 401 errors in the forums in the past and saying
> it would not work although they could successfully trap a 404 error.
> Any thoughts?
>