我使用带有身份2.0的ASP.NET MVC 5。我在Startup.cs中设置了身份会话超时,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/login"),
ExpireTimeSpan = TimeSpan.FromMinutes(double.Parse(ConfigurationManager.AppSettings["app:SessionTimeout"]))
});
我认为这个超时只是为asp.net auth cookie设置的。所以,我还改变了Global.asax中其他会话的会话超时,如下所示:
protected void Session_Start()
{
Session.Timeout = int.Parse(ConfigurationManager.AppSettings["app:SessionTimeout"]);
}
我使用以下代码登录我的登录逻辑(注意最后的Session变量):
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";
这就是问题:登录后一段时间,User.Identity.IsAuthenticated
返回true,User.Identity.GetUserId()
返回用户的ID,而Session["MyCustomData"]
返回null。似乎owin会话和自定义会话超时未同步。我该怎么做才能让他们同步?