2

I have a class with a subroutine as follows:

Public Sub SetPermissions()
    If IsNothing(HttpContext.Current.Session) Then
        Exit Sub
    Else
        Dim session As HttpSessionState = HttpContext.Current.Session
        If Not IsNothing(session("UserId")) Then '<-- exception occurs here
            'Do Stuff
        End If
    End If
End Sub

I know that my session variable is not yet set, hence a null value, that's why i'm trying to handle it with IsNothing, but my code still bugs out on me.

Any ideas? Cheers

8
  • I can't reproduce this behavior in a test project. Is the SetPermissions() method getting called from an HttpModule that is running before HttpContext.Current.Session is initialized? Commented Feb 2, 2012 at 3:30
  • What line is the exception on? Maybe HttpContext.Current is null and not HttpContext.Current.Session. Commented Feb 2, 2012 at 3:38
  • @SteveWellens: Sorry, the original post did indicate the line where the error occurred, I accidentally dropped it when I edited for formatting. It's back now. Commented Feb 2, 2012 at 3:42
  • I see..10 demerits anyways. My original question/point is still valid. Is session null at the point you access it? Commented Feb 2, 2012 at 5:37
  • setPermissions is being called in global.asax with by the following: "Sub Application_AuthenticateRequest(sender As Object, e As EventArgs) If Request.IsAuthenticated Then Dim _permissions As New Permissions.UserPermissions _permissions.SetPermissions() End If End Sub" Commented Feb 2, 2012 at 8:10

1 Answer 1

2

If you are going to try to access Session variables from Global.asax, you need to make sure you use an event that is fired after Session has been initialized for the current request. If you take a look at the documentation on MSDN, you'll see there are several events you can wire up in the Global.asax (check out the list of events in the section "The request is processed by the HttpApplication pipeline").

If you wire up those events, you should find that Session is initialized before Application_AcquireRequestState is raised. If you move your code out of Application_AuthenticateRequest and into Application_AcquireRequestState then it should work properly.

Note that I used the following code to test when Session would be initialized (C#, sorry). I wired up each event in order from the documentation, and Application_AcquireRequestState was the first event where I saw session == null evaluate to false.

void Application_AcquireRequestState(object sender, EventArgs e)
{
    var session = HttpContext.Current.Session;
    Response.Write("Application_AcquireRequestState: -> session is null: ");
    Response.Write(session == null);
    Response.Write("<br />");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Mate Cheers, that seems to do the trick, I went for the following in my asax file: Sub Application_PostAcquireRequestState( ) If HttpContext.Current.Request.IsAuthenticated Then Dim _permissions As New Permissions.UserPermissionsBLL _permissions.SetPermissions(User.Identity.Name) End If End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.