我似乎不清楚如何IsPersistent在OWIN cookie身份验证中工作,下面的代码是使用IsPersistent:
var context = Request.GetOwinContext();
var authManager = context.Authentication;
var properties = new AuthenticationProperties { IsPersistent = isPersistence };
authManager.SignIn(properties, identity);
Run Code Online (Sandbox Code Playgroud)
当用户检查/取消选中Remember me(IsPersistent后面使用)时,我没有看到区别,因为如果我关闭Chrome浏览器并再次打开它以与网站一起使用,则cookie .AspNet.ApplicationCookie仍然存在,即使我检查或取消选中它也可以让我进入Remember me.
我已检查链接IsPersistent上的定义:
获取或设置是否跨多个请求持久保存身份验证会话.
但是,因为我看到它仍然有效,所以不要太了解.
设置OWIN cookie身份验证的代码:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = ApplicationTypes.ApplicationCookie,
ExpireTimeSpan = TimeSpan.FromMinutes(30),
LoginPath = new PathString("/Account/LogOn")
});
Run Code Online (Sandbox Code Playgroud) 整天都在这个问题上摸不着头脑.我正在尝试在MVC Identity 2.0.1中设置"非常长"的登录会话.(30天).
我使用以下cookie启动:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
SlidingExpiration = true,
ExpireTimeSpan = System.TimeSpan.FromDays(30),
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/My/Login"),
CookieName = "MyLoginCookie",
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
Run Code Online (Sandbox Code Playgroud)
总的来说,工作正常.因此,cookie设置为30天,所有看起来都很好.
如果我关闭浏览器并在"validateInterval"持续时间过去之后回来(这里30分钟)我仍然登录,但是现在cookie仅作为"会话"重新发布(仍然是正确的cookie名称)!30天的到期消失了.
如果我现在关闭浏览器/再次重新打开,我将不再登录.
我已经测试了删除"提供者"并且所有工作都按预期进行,我可以在几个小时后回来并且我仍然可以正常登录.我读到最好使用邮票重新验证,所以我不确定如何继续.
我有一个用于用户身份验证的ASP.NET MVC 5应用程序ASP.NET Identity 2.1.0.
过去一切都运行良好,但现在我发现持久的用户会话不再起作用了.我不知道什么样的变化打破了这一点,但它工作时,我实现了身份(转换从应用程序SimpleMembership),这是我的逻辑我的那一刻:
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: true);
Run Code Online (Sandbox Code Playgroud)
SignInManager是我的ApplicationSignInManager基础SignInManager<ApplicationUser, int>和model.RememberMe是true.
我的设置:
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = ApplicationCookieIdentityValidator.OnValidateIdentity(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)
除了持久化用户会话外,一切正常.我检查了我的服务器返回的cookie,并且.AspNet.ApplicationCookie总是返回"当前会话有效"而不是将来的某个日期.因此,当我关闭并重新打开浏览器时,我需要再次登录...
有没有人知道为什么这不起作用了?
PS:我已经覆盖SignInAsync了我,ApplicationSignInManager因为我在那里做了一些自定义逻辑,但我甚至检查了调试器并进行了以下调用:
await base.SignInAsync(user, isPersistent, rememberBrowser);
Run Code Online (Sandbox Code Playgroud)
isPersistent是的 …
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2