[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2277
  • Last Modified:

ASP.NET: Using Global.asax to Check if User is Logged in When Each Page is Requested

I am having trouble using code in the Global.asax file to determine whether the user is logged in or not. I need to do this each time a page is requested.  I am using ASP.NET 4.0 with VB, and am using the membership feature to manage registrations and users.

I have tried this approach, where theIF statement runs without errors, but always returns false, even when the user is logged in.
    Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
                
        If Request.IsAuthenticated Then
            [Do something]
        Else
            [do something else]
        End If

Open in new window


I have also tried this, but this IF statement generates the "object reference not set to an instance of an object".
    Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
                
        If User.Identity IsNot Nothing And User.Identity.IsAuthenticated Then
            [Do something]
        Else
            [do something else]
        End If

Open in new window


Can anyone help make this work?  Or would a different approach be better, maybe something in an event of the Master page that checks if the user is logged in?  It needs to be pretty early, since the logic will control setting the thread culture for globalization.

Thanks!

P.S. Sorry these code snippets have so much white space ... I'd love to fix that for my posts here if you know how!
0
JMS1965
Asked:
JMS1965
  • 3
  • 2
1 Solution
 
MlandaTCommented:
Use AndAlso in your IF
        If User IsNot Nothing Andalso User.Identity IsNot Nothing AndAlso User.Identity.IsAuthenticated Then

Open in new window


AndAlso short circuits the IF so that if an early check fails, it does not try to evaluate the rest of the conditions.
0
 
JMS1965Author Commented:
Thank you for the suggestion. When I change the code as suggested, it runs without errors, but it still can't figure out if the user is authenticated ... it always evaluates the IF statement to false.

In my testing, I am debugging a page that requires a logged in user to access it. I set the breakpoint on the IF statement in global.asax. Then I debug the page that is secure. I immediately get sent to the Login page (as expected), and the IF statement in global.asax is evaluating false - as expected.

Then I complete the login process, and am automatically redirected to the orignal page requested. At this point, the IF statement still evaluates to False, but it should be true. I'm logged in, user name displayed in the header of the page, etc.

I thought this was a coding quesion .... but maybe it's more of a membership question?

Any thoughts?
0
 
MlandaTCommented:
Look at Scott Mitchells comments on this issue http://forums.asp.net/t/1177741.aspx/1
0
 
JMS1965Author Commented:
Thank you! Using the link to Scott Mitchell's post that you provided, I was finally able to work this out!

In case anyone reads this later, here's what worked for me:

In Account/Login.aspx.vb, I added this code:
    Protected Sub LoginUser_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoggedIn
        FormsAuthentication.SetAuthCookie(LoginUser.UserName, False)
    End Sub

Open in new window


Then in Global.asax, in Application_BeginRequest, I replaced my previous IF statement (trying to determine if the request is coming from a logged-in user) to this:

        Dim cookName As String = FormsAuthentication.FormsCookieName.ToString
        Dim cookAuth As HttpCookie = Request.Cookies.Get(cookName)
       
        If cookAuth IsNot Nothing Then

This worked ... thanks again!
0
 
JMS1965Author Commented:
Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now