Click here to Skip to main content
Licence CPOL
First Posted 17 Mar 2009
Views 26,055
Bookmarked 22 times

Bypass ASP.NET unauthorized redirect to a login page

By | 17 Mar 2009 | Article
How to bypass ASP.NET unauthorized redirect to a login page.

Introduction

ASP.NET makes it easy to configure Forms Authentication and Authorization, including automatically redirecting you to the login page when necessary. The problem is that it also redirects authenticated users to the login page when they attempt to access pages that they are not authorized to access. This gives you the opportunity to login as someone else, and then be automatically redirected back to the page you originally attempted to access. But, that may not be the behavior you want for authenticated users -- do your users really have multiple logins, and do they understand why they end up back at the login page?

Lots of my system users contacted me about this behavior, they thought it is a bug and needs to be fixed!

The Solution

After lots of attempts, I found an acceptable approach (for me); it is all in the Global.asax Application_EndRequest event.

Protected Sub Application_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs)
    Try
       'Check if the user is Authenticated and been redirected to login page
       If Request.IsAuthenticated  _
       And Response.StatusCode = 302  _
       And Response.RedirectLocation.ToUpper().Contains("LOGIN.ASPX") _
       Then
             ' check if the user has access to the page
            If Not UrlAuthorizationModule.CheckUrlAccessForPrincipal _
                                        (Request.FilePath, User, "GET") Then
                'Pass a parameter to the login.aspx page 
                FormsAuthentication.RedirectToLoginPage("errCode=401")
                
                'Or you can redirect him to another page like AuthenticationFaild.aspx
                'Response.Redirect("AuthenticationFaild.aspx")
            End If
        End If
    Catch ex As Exception
        'Do nothing
    End Try
End Sub

Basically, I check to see if the response is a redirect to the login page and if the user has already been authenticated. Finally, I check to see if the user does not have access from the original requested page. If all of those conditions are true, then I redirect them to the login page with parameters to indicate it's an authorization redirect.

Anyway, I hope this helps someone.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ala Hamad

Software Developer (Senior)
LexisNexis
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generallogin.aspx PinmemberAjay Kale New0:26 27 Sep '10  
Questionre automatic redirection to Login.aspx PinmemberAjay Kale New2:32 9 Sep '10  
Generalat least you tried. PinmemberDonsw7:13 10 Apr '09  
GeneralMy vote of 1 Pinmembernsimeonov12:25 24 Mar '09  
GeneralThe Login.aspx is configurable PinmemberTimMerksem6:19 24 Mar '09  
GeneralRe: The Login.aspx is configurable PinmemberAla Hamad8:01 24 Mar '09  
GeneralRe: The Login.aspx is configurable PinmemberTimMerksem6:13 26 Mar '09  
GeneralRe: The Login.aspx is configurable PinmemberAla Hamad6:54 26 Mar '09  
GeneralRe: The Login.aspx is configurable PinmemberTimMerksem6:57 26 Mar '09  
GeneralRe: The Login.aspx is configurable PinmemberMichael J. Collins4:34 24 Jun '09  
GeneralRe: The Login.aspx is configurable PinmemberAla Hamad10:35 17 Aug '09  
GeneralNew Code PinmemberFabio Galante Mans8:26 17 Mar '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Mar 2009
Article Copyright 2009 by Ala Hamad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid