I'm having a pretty tough time with this issue. I've done a lot of research but haven't found anything that works!
What I want to do: 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.
What is working: 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:
<script type="text/javascript">
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);
};
</script>
What is not working: 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.
Here is a snipped of code from my sign in action in my controller:
var claims = UserManager.GetClaims(user.Id);
var timezoneclaim = claims.First(c => c.Type == "UserTZ");
UserManager.RemoveClaim(user.Id, timezoneclaim);
var timezoneoffsetclaim = claims.First(c => 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()));
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.
I've tried:
- Adding remove name="Session" and add name="Session" type="System.Web.SessionState.SessionStateModule" to my webconfig based on a similar issue I found here.
- I tried adding the Microsoft.Aspnet.Session nuget package and following this tutorial but my project's startup.cs doesn't have a ConfigureServices section and adding my own didn't work either.
- I set the session state to InProc in the webconfig based on what I found here but again, nothing changed.
Thoughts?