2015-12-22 78 views
6

使用ASP.NET身份时,我想在用户登录到我的网站时尽可能长地保存登录信息,以便用户在重新打开浏览器时不需要再次登录(就像github.com和stackoverflow.com)。当我登录github时,它会持续多天的信息,所以我不需要每天重新登录。有什么方法可以使用ASP.NET身份来实现此功能吗?如何使用asp.net标识关闭浏览器后保存登录信息?

回答

3

适当的值就传递到的签到方法isPersistent说法:

SignInManager.PasswordSignInAsync("[email protected]", "password", isPersistent: true, shouldLockout: false); 

SignInManager.SignInAsync(applicationUser, isPersistent: true, rememberBrowser: false); 

isPersistent参数用于控制,如果用户的验证cookie应持久。

rememberBrowser参数用于双因素身份验证:记住的浏览器可以直接使用密码直接登录。

0

您需要在AuthenticationProperties上设置IsPersistent属性,以便在浏览器关闭后继续登录。注册方法user.GenerateUserIdentityAsync生成新的ClaimsIdentity以在Cookie中刷新。

private async Task<SignInStatus> SignIn(User user, bool isPersistent) 
{ 
    await SignInAsync(user, isPersistent); 
    return SignInStatus.Success; 
} 

public async Task SignInAsync(User user, bool isPersistent) 
{ 
    var userIdentity = await user.GenerateUserIdentityAsync(UserManager); 
    AuthenticationManager.SignIn(
     new AuthenticationProperties 
     { 
      IsPersistent = isPersistent 
     }, 
     userIdentity 
    ); 
}