Solved

VB.NET Session Variable not working

Posted on 2014-09-14
10
622 Views
Last Modified: 2014-10-12
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?
0
Comment
Question by:thinklings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40322104
perhaps your not setting the session variable access granted to true
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40322217
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.
0
 

Author Comment

by:thinklings
ID: 40322255
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?
0
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!

 

Author Comment

by:thinklings
ID: 40322352
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.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40322355
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.
0
 

Author Comment

by:thinklings
ID: 40322359
When attempting to use your code, I get the following warning error:

'TryCast' operand must be reference type, but 'Boolean' is a value type
0
 

Author Comment

by:thinklings
ID: 40322373
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

0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40324540
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.
0
 

Accepted Solution

by:
thinklings earned 0 total points
ID: 40367411
Thanks, MIguel but it still didn't work.

I just changed to using .net authentication instead.
0
 

Author Closing Comment

by:thinklings
ID: 40375439
Had to opt for a different solution rather than fixing the problem.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question