0

I am a bit new to VB.NET. I have a page that sets 2 session variables and does a redirect to second page. The second pages is at least using one of the session variables. I can tell because on the second page, if the session variable is not correct the user is redirected to an access denied page. The second page also uses the session variable in question. It will read it an fill a gridview based on the value of the variable. I set the variable like so

Session("ID") = Convert.ToInt32(a_value) 

and on the second page I retrieve the variable like this

a_page_variable = Session("ID")

What I find strange is that when I run this code in visual studio it works as expected but when I deploy and run it, I get 0 from my session variable instead of the true value of "a_value". I have tried a few things like making sure the data types match up from page to page and trying different ways to retrieve the variable such as

Session("userID")

and

CType(Session.Item("userID"), Int32)

I've also tried to see what is coming in to the second page by using Response.Write I also tried to use SQL Profiler to see what kind of call is being made to fill the gridview but I haven't had any luck. The gridview gives me an empty dataset and the Profiler does not detect a call being made from the application. I thought working with session variables was pretty straight forward but obviously, I am missing something.

Thanks for your help, Billy

Tony Dong
  • 3,213
  • 1
  • 29
  • 32
billy_blanks
  • 105
  • 1
  • 3
  • 12
  • how is the session storage configured in your deployed web.config? What is the deployment environment like? e.g. Is there perhaps one more than IIS server (e.g. in a cluster, where each request might be redirected to a different physical server by a load balancer - I ask this because in that scenario the servers need to share session data, perhaps stored in a database)? Without config and code info it's hard to give specific advice. Descriptions of code are useful but they are quite hard things to actually fix! – ADyson Mar 20 '19 at 22:12
  • I'm kinda new to .NET and I don't know how to answer the first question. How can I find out? As far as the deployment environment, there is only one IIS server – billy_blanks Mar 20 '19 at 22:23
  • By looking in your web.config file. https://www.c-sharpcorner.com/UploadFile/484ad3/session-state-in-Asp-Net/ might help you understand the different session storage options and what to look for in the file. – ADyson Mar 20 '19 at 22:27
  • Thank you. I'll check that out. – billy_blanks Mar 20 '19 at 22:50
  • @ADyson I tried adding the sessionState to my web.config file but I am still having the same problem. It looks like InProc mode is the default behavior and the recommended setting. – billy_blanks Mar 20 '19 at 23:08
  • 1
    ok then well we can seemingly rule that out then. As the answer below notes, there are a number of other possible causes for session issues. As it seems to be an environment problem (i.e. it works in local environment but not in the server) then it's less likely that your code is at fault. Since we don't have any visibility of your environment, it's really quite hard to do anything more than make suggestions for possible areas to search in. I haven't really got anything more to add than what's already below, sorry. – ADyson Mar 20 '19 at 23:20
  • If you set [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) for the project ([specific steps here](https://stackoverflow.com/q/5076851/1115360)) then perhaps correcting the problems it points out will make it work. – Andrew Morton Mar 21 '19 at 11:08
  • ... and if it still doesn't work, several people share possible solutions in [Losing session data in ASP.NET](https://stackoverflow.com/q/19871081/1115360). – Andrew Morton Mar 21 '19 at 11:12
  • @Andrew I've tried setting the options that you've suggested to On. I did have to make a couple of changes because of the Strict option but that didn't help. I am just at a loss as to why one variable can be set, passed and read on the next page but the next one cannot. Kinda frustrating. Using Response.Write on the second page, I can display the passed value of my first variable but the second variable is just blank. – billy_blanks Mar 21 '19 at 16:09
  • @billy_blanks Carefully make sure that the name really is exactly the same. (I noticed that you showed both `Session("ID")` and `Session("userID")` in the question.) – Andrew Morton Mar 21 '19 at 16:13

2 Answers2

2

One possibility (and the only one that could be guessed at with how little information we have) could be the response.redirect causing the application to terminate due to an exception.

When redirecting, you want to always pass a false, and then call complete request.

Response.Redirect(urlstring, False)
Response.CompleteRequest()

not following these steps can cause exceptions, which may drop session.

Additionally, resolve virtual paths, as some browsers (mobile especially) can see those redirects as new requests entirely, thus generating new session tokens.

Dim urlstring As String
urlstring = Page.ResolveUrl("~/default.aspx")

that said, there are a number of possible causes for this situation.

  1. Application Pool restarts
  2. App Domain restarted
  3. Code changing value unexpectedly
  4. AV tinkering with files
  5. deployed to web farm

With the description provided above, we just don't have enough information to really troubleshoot.

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
  • Thank you for your input Stephen. I've tried resolving the url and adding false to the redirect but I am still having the same issues. I am setting and retrieving the session variables in the Page_Load events on both pages. – billy_blanks Mar 20 '19 at 22:41
  • Thank you for your input Stephen. I've tried resolving the url and adding false to the redirect but I am still having the same issues. I am setting and retrieving the session variables in the Page_Load events on both pages. This is from the first page `Session("ID") = Convert.ToInt32(a_value) urlString = Page.ResolveUrl("~/User.aspx") Response.Redirect(urlString, False)`. On the second page, I have this `a_page_variable = Session("ID") SqlSource.SelectParameters("ID").DefaultValue = a_page_variable`. – billy_blanks Mar 20 '19 at 22:49
  • Stephen, I don't have any of the items you listed going on. There is one application pool, the domain has not been restarted and the application has not been deployed to a web farm. I haven't received any messages from the AV. Is there any other information that I can give that would be help out? – billy_blanks Mar 20 '19 at 23:14
  • Really nice answer. I have a similar program, but not the same. My session values work fine at home...however when I use them on my LAN of 6 computers at the office. The first 6 users dont have any issues and the sessions work fine...however when they change to the next 6 users and they log in to their accounts..the sessions vars become null but eventually go back to normal after logging in 2-4 more times...and odd error that I can't understand...any ideas? – F U Feb 04 '21 at 15:48
1

Thank you ADyson, Stephen Wrighton and everyone else who took a stab at helping me figure this out. I was able to find out what was going on by adding code that wrote to a log file on the server. Found the logging code here. I found that I never reached the code that set the session variable and that is the reason it never populated on the second page. I was trying to get the logon name for the user by using Environment.UserName which will return the user name of the person who is currently logged on to the operating system. But what I really wanted to do was get the logon name of the user that was visiting my site. For this I used User.Identity.Name. This works great when you need to know which user from an Active Directory domain is visiting your site.

billy_blanks
  • 105
  • 1
  • 3
  • 12