Click here to Skip to main content
15,881,715 members
Articles / Web Development / ASP.NET

Bypass ASP.NET unauthorized redirect to a login page

Rate me:
Please Sign up or sign in to vote.
4.21/5 (10 votes)
17 Mar 2009CPOL1 min read 61.9K   25   12
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.

VB
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)


Written By
Software Developer (Senior) LexisNexis
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generallogin.aspx Pin
Ajay Kale New27-Sep-10 0:26
Ajay Kale New27-Sep-10 0:26 
Questionre automatic redirection to Login.aspx Pin
Ajay Kale New9-Sep-10 2:32
Ajay Kale New9-Sep-10 2:32 
Generalat least you tried. Pin
Donsw10-Apr-09 7:13
Donsw10-Apr-09 7:13 
GeneralMy vote of 1 Pin
nsimeonov24-Mar-09 12:25
nsimeonov24-Mar-09 12:25 
GeneralThe Login.aspx is configurable Pin
TimMerksem24-Mar-09 6:19
TimMerksem24-Mar-09 6:19 
GeneralRe: The Login.aspx is configurable Pin
Ala Hamad24-Mar-09 8:01
Ala Hamad24-Mar-09 8:01 
GeneralRe: The Login.aspx is configurable Pin
TimMerksem26-Mar-09 6:13
TimMerksem26-Mar-09 6:13 
GeneralRe: The Login.aspx is configurable Pin
Ala Hamad26-Mar-09 6:54
Ala Hamad26-Mar-09 6:54 
GeneralRe: The Login.aspx is configurable Pin
TimMerksem26-Mar-09 6:57
TimMerksem26-Mar-09 6:57 
GeneralRe: The Login.aspx is configurable Pin
Michael J. Collins24-Jun-09 4:34
professionalMichael J. Collins24-Jun-09 4:34 
GeneralRe: The Login.aspx is configurable Pin
Ala Hamad17-Aug-09 10:35
Ala Hamad17-Aug-09 10:35 
GeneralNew Code Pin
Fabio Galante Mans17-Mar-09 8:26
Fabio Galante Mans17-Mar-09 8:26 

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

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