How to persist the login information after closing Browser using asp.net Identity?

你说的曾经没有我的故事 提交于 2020-01-02 03:18:47

问题


When using the ASP.NET Identity, I want to persist the login information as long as possible when the user logs in to my website, so the user doesn't need to login again when they reopened their Browser (just like github.com and stackoverflow.com). When I login to github, it persists my information for many days, so I don't need to login again every day. Are there any methods can that can implement this functionality using ASP.NET Identity?


回答1:


Just pass the appropriate value to the isPersistent argument of the SignIn methods:

SignInManager.PasswordSignInAsync("email@id.com", "password", isPersistent: true, shouldLockout: false);

or

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

The isPersistent argument is used to control if the user's authentication cookie should be persisted.

The rememberBrowser argument is used in case of Two Factor Authentication: a remembered browser can login directly with the password alone.




回答2:


You need to set IsPersistent property on AuthenticationProperties to persist login after browser close. The registered method user.GenerateUserIdentityAsync generates the new ClaimsIdentity to be refreshed in the 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
    );
}


来源:https://stackoverflow.com/questions/34407735/how-to-persist-the-login-information-after-closing-browser-using-asp-net-identit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!