title: How to retrieve browser session data in ASP.Net MVC Controller?
body: <p>I'm having a pretty tough time with this issue.  I've done a lot of research but haven't found anything that works!</p>
  
  <p><strong>What I want to do:</strong>
  I want to use javascript to capture the user's timezone and utc timezone offset, store these two pieces of data in the browsers session, and then retrieve them in the controller.</p>
  
  <p><strong>What is working:</strong>
  My javascript is working to extract the user's timezone and timezone offset and save these two variables into the session.  The following is my working JS:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
   window.onload = function () {
       var UserTime = new Date();
       var stringtz = UserTime.toString();
       var tzstart = stringtz.indexOf("(") + 1;
       var tzend = stringtz.indexOf(")");
       var userTZ = stringtz.slice(tzstart, tzend);
       sessionStorage.setItem("userTZOffset", UserTime.getTimezoneOffset().toString());
       sessionStorage.setItem("userTZ", userTZ);
   };
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>What is not working:</strong>
  When the user signs in, I want to grab the two variables (userTZ and userTZOffset) saved in the session and create claims for them.  However, when I try to pull these into my controller I get null as the values.  Upon further inspection, I can see the session timeout, id, etc but the keys and values are blank.</p>
  
  <p>Here is a snipped of code from my sign in action in my controller:</p>
  
  <pre><code>        var claims = UserManager.GetClaims(user.Id);
  
          var timezoneclaim = claims.First(c =&gt; c.Type == "UserTZ");
          UserManager.RemoveClaim(user.Id, timezoneclaim);
  
          var timezoneoffsetclaim = claims.First(c =&gt; c.Type == "UserTZOffset");
          UserManager.RemoveClaim(user.Id, timezoneoffsetclaim);
  
          var test = HttpContext.Session["userTZ"];
          var test2 = HttpContext.Session["userTZOffset"];
  
          UserManager.AddClaim(user.Id, new Claim("UserTZ", test.ToString()));
          UserManager.AddClaim(user.Id, new Claim("UserTZOffset", test2.ToString()));
  </code></pre>
  
  <p>I set breakpoints on this and walk through the code but everytime, test and test2 are null.  When I look into the session itself on the breakpoints I see SessionId, Timeout, etc but the count is 0 and the Key count is 0.</p>
  
  <p>I've tried:</p>
  
  <ol>
  <li>Adding remove name="Session"  and  add name="Session" type="System.Web.SessionState.SessionStateModule" to my webconfig based on a similar issue I found <a href="https://stackoverflow.com/questions/218057/httpcontext-current-session-is-null-when-routing-requests">here</a>.</li>
  <li>I tried adding the Microsoft.Aspnet.Session nuget package and following <a href="http://benjii.me/2015/07/using-sessions-and-httpcontext-in-aspnet5-and-mvc6/" rel="nofollow noreferrer">this</a> tutorial but my project's startup.cs doesn't have a ConfigureServices section and adding my own didn't work either.</li>
  <li>I set the session state to InProc in the webconfig based on what I found <a href="https://stackoverflow.com/questions/28081004/mvc-5-session-is-null">here</a> but again, nothing changed.</li>
  </ol>
  
  <p>Thoughts?</p>