views:

825

answers:

3

Hi, Im using outproc session that is managed by aspnet_state. Sometimes I get run time errors saying that the session is invalid. So I wanted to check if the session is valid for every request I make. But I couldn't find a proper way to do it like in this Question using Java.

Here is the code I'm using right now in the page_preinit event.It looks ugly but it works.

            Try
                Dim x = Session.Keys().Item(0)
            Catch ex As Exception
                Session.Clear()
            End Try

Does any one knows a better approach?

Thanks

+1  A: 

You could try checking the context object.

C#

if(Context.Session != null)
{
    //Redirect to login page etc
}

VB (Used a C# to VB converter here..not sure if this is correct)

If Context.Session IsNot Nothing Then
    'Redirect to login page etc
End If

If you are using forms authentication and it is setup correctly, it should redirect for you. If you still need to be explicit, I would recommend placing this type of code in the Global.asax, or a base class that each of your pages could derive from, rather than adding it to every request.

PortageMonkey
The code I'm using is on the base page. But the session does exist it is not null. Accessing eny element will throw an exception. By simply restarting the asp_net session the problem goes or in the code I clean it. The code I'm using does solve my problem but Iwas wondering if there is other to do it.
Youssef
Hmm. this sounds like you have a problem with the session information (keys) not being populated correctly...rather than the session timing out. We have had similar problems using the aspnet_state DB. Our approach has been to attempt to access a session key in addition to checking for null in order to validate that the aspnet_state DB returned a valid session object. As to the underlying problem with a bad session being returned by aspnet_state DB...still have yet to solve that one
PortageMonkey
A: 

Actually I solved the problem by checking the first element of the session( session.item(0) ) I put the code in try catch . if the there is an exception I just clear the session. This code is in a basepage for all my pages.

so in the preinit event my code look like this

Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
    Try
     Dim x = Session.Keys().Item(0)
    Catch ex As Exception
     Session.Clear()
    End Try
End Sub

It seems to be ugly but it does solve my problem.

Thanks everyone.

Youssef
A: 

Just check the Session.Count instead of attempting to access the first item in an error handling block...

dudeNumber4
No, the count did not help as the count was correct.The session has elements in it, but they are corrupted for some reason. You can replicate that by using out proc session after it runs for some times(heavily accessed) it will start crashing. It could be memory problem.I didn't investigate it throughly but the try catch works fine for me.
Youssef