我似乎不清楚如何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) 我使用asp.net identity 2.0来管理用户登录.我正在关注Identity 2.0的示例,并且在整个浏览器关闭后无法使cookie保持不变.这种情况发生在所有浏览器上.
码:
账户管理员
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInHelper.PasswordSignIn(model.Email, model.Password, isPersistent: true, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
SignInHelper
public async Task<SignInStatus> PasswordSignIn(string userName, string password, bool isPersistent, bool shouldLockout)
{
var user = await UserManager.FindByNameAsync(userName);
if (user == null)
{
return SignInStatus.Failure;
}
if (await …Run Code Online (Sandbox Code Playgroud)