2015-12-22 77 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 
    ); 
}