AuthenticateRequest always fires

Last post 04-11-2006 6:42 AM by Ashiki. 6 replies.

Sort Posts:

  • AuthenticateRequest always fires

    04-06-2006, 5:56 AM
    • Member
      145 point Member
    • Ashiki
    • Member since 03-24-2006, 4:58 AM
    • Stamford, UK
    • Posts 43

    I have a HTTP Module that grabs the AuthenticateRequest and checks the current user's credentials. It all works fine and I thought that I could specify the pages that should be under this system by using web.config like so:

    <location path="SignUp.aspx">
    <system.web>
    <authorization>
    <allow users="*" />
    </authorization>
    </system.web>
    </location>

    <location path="Users.aspx">
    <system.web>
    <authorization>
    <deny users="?" />
    </authorization>
    </system.web>
    </location>

     

    .. in which case users.aspx would require authorization but signup.aspx would not. It seems that AuthenticateRequest fires on every page no matter what and there's no method I can find to check whether to run code - such as a FormsAuthentication.IsRequired, or something like that. I've tried using AuthorizeRequest instead but that gives the same result and I've found the UrlAuthorizationModule.CheckUrlAccessForPrincipal method in 2.0, but I'm using 1.1 !

    So, how do you check if authorization is required?

     

    'example code from httpmodule

    Private Sub Application_AuthenticateRequest(ByVal Source As Object, ByVal e As EventArgs)

    'do stuff here

    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init

    AddHandler context.AuthenticateRequest, AddressOf Me.Application_AuthenticateRequest

    End Sub

     

  • Re: AuthenticateRequest always fires

    04-06-2006, 3:43 PM
    • Contributor
      3,067 point Contributor
    • sschack
    • Member since 09-16-2003, 4:06 PM
    • Posts 613
    • AspNetTeam
      Moderator

    AuthenticateRequest is an event that occurs in the Http pipeline - which is why an event handler that subscribes to this will get called on each request.  Usually this is the event where code looks for some value in the request (a cookie, a header, etc...) and if this value is found, converts it into a principal object that is placed on the HttpContext.

    If the intent is to determine if authorization failed, and a redirect is needed to a login page, you can hook the EndRequest event.  If the value of Response.StatusCode is 401, this indicates that an authorization failure occurred (probably from UrlAuthorization).  In this case you can then redirect to an appropriate login page.

    -Stefan
    ----------------------------------------------------------
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: AuthenticateRequest always fires

    04-07-2006, 6:40 AM
    • Member
      145 point Member
    • Ashiki
    • Member since 03-24-2006, 4:58 AM
    • Stamford, UK
    • Posts 43

    I always get statuscodes of 200 or 302 from EndRequest. I think this is because the page is either available to unauthorized users (200) or the user is being redirected by forms authentication (302) and then landing on the login.aspx page (200) (which I haven't configured as "login.aspx" in web.config, that just seems to be the default). So I guess what I want to do is to get access to the HTTP pipeline before forms authentication does - anyone know how to do that? I've tried the code below but without success:

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init

    Dim instance As FormsAuthenticationModule

    AddHandler instance.Authenticate, AddressOf  Me.Forms_AuthenticateRequest

    End Sub

     

    Private Sub Forms_AuthenticateRequest(ByVal Source As Object, ByVal e As Web.Security.FormsAuthenticationEventArgs)

    'do stuff here

    End Sub

  • Re: AuthenticateRequest always fires

    04-07-2006, 1:57 PM
    • Contributor
      3,067 point Contributor
    • sschack
    • Member since 09-16-2003, 4:06 PM
    • Posts 613
    • AspNetTeam
      Moderator
    I guess the question is what do you need to do in the app?  If you just want to change the login page, you can set the "loginUrl" attribute on the <forms /> element to your sign in page.
    -Stefan
    ----------------------------------------------------------
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: AuthenticateRequest always fires

    04-10-2006, 4:03 AM
    • Member
      145 point Member
    • Ashiki
    • Member since 03-24-2006, 4:58 AM
    • Stamford, UK
    • Posts 43
    The login page is on another domain. That's why I want to handle the page request before forms authentication does, but only if the page is configured in web.config to disallow anonymous access.
  • Re: AuthenticateRequest always fires

    04-10-2006, 5:09 PM
    • Contributor
      3,067 point Contributor
    • sschack
    • Member since 09-16-2003, 4:06 PM
    • Posts 613
    • AspNetTeam
      Moderator

    It looks like the forms authentication module is always going to get in the way.  I see two solutions to this:

    1.)  Hook the EndRequest event.  Look for a Response.StatusCode of 302 and Response.RedirectLocation set to the login page for the application.  This combination of data indicates that the current request failed authorization to the requested page.

    2.)  Set the "loginUrl" attribute for your application to the remote application that has the real login page:  <forms ... loginUrl="http://www.myloginapp.com/login.aspx" />

    -Stefan
    ----------------------------------------------------------
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: AuthenticateRequest always fires

    04-11-2006, 6:42 AM
    • Member
      145 point Member
    • Ashiki
    • Member since 03-24-2006, 4:58 AM
    • Stamford, UK
    • Posts 43

    I think that's the answer! I've set the loginUrl in web.config to the external site and handle the FormsAuthenticationModule Authenticate event in a HTTP Module to check for authenticated users being redirected back from the external site. It seems to work fine.

Page 1 of 1 (7 items)