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();
orSession.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