I'm using ASP.NET MVC 5 with identity 2.0. I've set identity session timeout in Startup.cs as below:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/login"),
ExpireTimeSpan = TimeSpan.FromMinutes(double.Parse(ConfigurationManager.AppSettings["app:SessionTimeout"]))
});
I think this timeout is just set for asp.net auth cookie. So, I've also changed session timeout for other sessions in Global.asax as below:
protected void Session_Start()
{
Session.Timeout = int.Parse(ConfigurationManager.AppSettings["app:SessionTimeout"]);
}
I'm using the following code for my login logic (Note the Session variable at the end):
var userManager = new UserManager();
var identityUser = await userManager.FindByIdAsync(UserID);
var identity = await userManager.CreateIdentityAsync(identityUser, DefaultAuthenticationTypes.ApplicationCookie);
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = IsPersistent }, identity);
Session["MyCustomSession"] = "MyCustomData";
This is the problem: after login and a period of time, User.Identity.IsAuthenticated
returns true, User.Identity.GetUserId()
returns user's id, while Session["MyCustomData"]
returns null. It seems that owin session and custom session timeout are not synced. What do I do to make them synced?