Link to home
Start Free TrialLog in
Avatar of thinklings
thinklings

asked on

VB.NET Session Variable not working

Hi,

I have a redirect setup on all pages of a .net web app so that on page load the following is checked to ensure the user is logged in correctly -

 If Session("AccessGranted") = False Then
            Response.Redirect("/Site/Login/Login.aspx")
End If

It seems as if even though Access Granted should be set to True, it keeps picking up that it is false instead and redirecting when it shouldn't be. Any idea why or if there is something I should be doing?
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

perhaps your not setting the session variable access granted to true
Session("AccessGranted") returns an object not a Boolean value, you need to cast it to Boolean as follows:
Dim enable As Boolean = TryCast(Session("AccessGranted") , Boolean)
If (enable  Is Nothing) Or (enable = False) Then
             Response.Redirect("/Site/Login/Login.aspx")
 End If

Open in new window


Note: You should set Session("AccessGranted") = True somehere in your code.
Avatar of thinklings
thinklings

ASKER

Thanks for your responses. I am setting the variable to True when they successfully enter their login details.

Miguel - I use the session variable, Access Granted, to pass a value between different pages so where am I supposed to declare the boolean - enable?
Not sure if this helps but the Session variable works fine locally on my dev computer but it is when I upload the application online via a shared hosting provider that the Session variable does not hold.
Replace your existing posted code with my code. Enable is only a local variable that is helping to determine the exact Session contents.

Regarding your last comment, it is a slightly different problem, check that in your web.config file in your server Session is enabled.  (<sessionState> key).
Also check that your server contains the same .NET framework version or later and all required prerequisites for your site.
When attempting to use your code, I get the following warning error:

'TryCast' operand must be reference type, but 'Boolean' is a value type
I have the following code within my web.config file. I am assuming this enables the sessionState.

<sessionState mode="InProc" cookieless="true" timeout="60"/>

Open in new window

Try:
If (CBool(Session("AccessGranted"))  = False) Then
             Response.Redirect("/Site/Login/Login.aspx")
 End If

Open in new window

CBool handles the case that session is Nothing.

Regarding web.config: Are both configuration files have the same sessionstate attributes?
Regarding server: Does the server contains the same .NET framework version or later and all required prerequisites for your site.
ASKER CERTIFIED SOLUTION
Avatar of thinklings
thinklings

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Had to opt for a different solution rather than fixing the problem.