2

I am running an ASP MVC site locally (.Net 4.5) and am experincing an issue when trying to retrieve a value from session. I am calling the code from a static helper class similar to the following:

Helpers.cs

public static void SetSessionValue(string key, object value)
{       
    HttpContext.Current.Session[key] = value;
}

public static object GetSessionValue(string key)
{   
    return HttpContext.Current.Session[key];
}

AjaxController.cs

public ActionResult SetUserName(string username)
{
    Helpers.SetSessionValue("username", username);
}

public ActionResult GetUserName()
{
    var username = Helpers.GetSessionValue("username"); 
    return Json(new { valid = true, username });
}

The username above is an example, but I have multiple cases of this happening, and each time the value for the key is null, but the key persists. I further went on and added the following to SetSessionValue to test:

public static void SetSessionValue(string key, object value)
{       
    HttpContext.Current.Session[key] = value;
    var test = HttpContext.Current.Session[key];
}

The variable test would be populated with the value. I double checked all the variable names. The keys still exist in the collection, but the values disappear.

Attempted Solutions

Solution 1 - HttpContext.Current.Session null item

I set the following in my web config, no luck

<sessionState mode="InProc" timeout="20" />

Solution 2 - HttpContext.Current.Session is null when routing requests

I set the following in my web config, no luck

<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>

Other Items I've tried

  • Made sure <httpCookies requireSSL="true" /> wasn't in the web.config
  • Tried enabling and running with SSL
  • Running in release mode
  • Ensured no Session.Abandon(); or Session.Clear();
  • Made sure no virus scans were running
  • Did a search to make sure I wasn't updating elsewhere
  • Ensured my ASP.NET State Service was running
  • Tried adding in [WebMethod(EnableSession = true)]
  • I've ensured I have cookies enabled
Community
  • 1
  • 1
Jacob Saylor
  • 2,371
  • 1
  • 31
  • 46
  • 1
    check if cookies are enabled in your browser – Akshey Bhat May 26 '16 at 03:56
  • Just checked again to make sure, they are... – Jacob Saylor May 26 '16 at 03:58
  • any chance you are actually invoking SetSessionValue/SetUserName with null for the second parameter? maybe add some logging when SetSessionValue is invoked. – xianwill May 26 '16 at 04:00
  • I've stepped through to make sure. Plus tested in the updated SetSessionValue above. I'm sure I'm passing it a value, just seems like it loses it after it leaves the method – Jacob Saylor May 26 '16 at 04:02
  • You are not setting username variable in GetUserName method..please update code – sangram parmar May 26 '16 at 04:06
  • I've updated the code for completeness sake. I'm setting it to a variable in my full implementation. – Jacob Saylor May 26 '16 at 04:09
  • You're gettting HttpContext.Current.Session is null or HttpContext.Current.Session["username"] is null? – Palanikumar May 26 '16 at 04:13
  • HttpContext.Current.Session is not null, and has all the keys still. I set a breakpoint to confirm. I tried HttpContext.Current.Session["username"] in the immediate window and that came back as null. – Jacob Saylor May 26 '16 at 04:15
  • Then check the HttpContext.Current.Session.SessionID is same for both SetUserName request and GetUserName request. – Palanikumar May 26 '16 at 04:17
  • I've confirmed in Fiddler they are the same – Jacob Saylor May 26 '16 at 04:19
  • Have you seen [Why I lose my session variable?](http://stackoverflow.com/questions/24534343/why-i-lose-my-session-variable) This might be due to using `InProc` session state (which BTW [isn't very useful for production use](https://brockallen.com/2012/04/07/think-twice-about-using-session-state/)) and app pool recycling (which could be from application compilation). – NightOwl888 May 26 '16 at 05:58
  • Also, you might want to [try this](http://stackoverflow.com/questions/5844635/session-variables-lost-between-controllers-action-methods#6305647). – NightOwl888 May 26 '16 at 06:00

2 Answers2

3

Found the issue... and feel like an idiot. Even though I went and closed all the instances in IIS Express and my running Services, there was another instance of the project being ran somewhere, thus confusing my session items.

To make sure I had everything up to date, I went in and modified the port number (Right click Project -> Properties)

enter image description here

After doing so, everything worked as expected. This might be a very unique case, but wanted to post just in case someone might run into a similar situation.

Jacob Saylor
  • 2,371
  • 1
  • 31
  • 46
0

You've almost checked everything, just check with cookieless mode ,see what comes up, I guess there is something wrong with your cookie stuff :

<sessionState mode="InProc" cookieless="true" />
akardon
  • 43,164
  • 4
  • 34
  • 42