C# ASP.NET web api“;“记住我”;使用cookie的功能

C# ASP.NET web api“;“记住我”;使用cookie的功能,c#,asp.net-web-api,C#,Asp.net Web Api,我正在尝试在我的Web Api项目中实现“记住我”功能 我想: 当用户登录时,具有“记住我”功能 为保存一个cookies,以保持用户始终登录,这样用户无需在每次访问网站时键入用户名和密码 通过读取上次登录时保存的cookies登录用户 我想的另一个问题是。。。当用户选中“记住我”复选框时,我试图使用JavaScript生成cookies。有可能这样做吗 或 我应该在AccountController中实现RememberMe() 补充: 这是我在ApplicationAuthProvide

我正在尝试在我的Web Api项目中实现“记住我”功能

我想:

  • 当用户登录时,具有“记住我”功能
  • 为保存一个cookies,以保持用户始终登录,这样用户无需在每次访问网站时键入用户名和密码
  • 通过读取上次登录时保存的cookies登录用户
我想的另一个问题是。。。当用户选中“记住我”复选框时,我试图使用
JavaScript
生成cookies。有可能这样做吗

我应该在
AccountController
中实现
RememberMe()

补充: 这是我在
ApplicationAuthProvider
中的代码

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

        ApplicationUser user = await userManager.FindByNameAsync(context.UserName);

        if (user == null) {...}

        if (userManager.IsLockedOut(user.Id)) {...}

        if (!(await userManager.CheckPasswordAsync(user, context.Password)))
        { ... }

        if (!user.EmailConfirmed) {...}

        ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
           OAuthDefaults.AuthenticationType);
        ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
            CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(user.UserName);
        AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
        context.Validated(ticket);
        context.Request.Context.Authentication.SignIn(cookiesIdentity);

您需要在应用程序中实现刷新令牌,才能提供此功能

基本上,您需要创建一个将生成刷新令牌的
RefreshTokenOAuthProvider
。您可以使用两种类型的
client\u id
来区分需要记住或不需要记住的客户端


在中对其进行了解释(尽管它可能会开始变得有点过时,但关于owin设置的信息是金色的)

我认为你的链接不正确。是对的吗
$('#checkbox').click(function () {

    if ($('#checkbox').is(':checked')) {
        // save username and password
        username = $('#txtLoginEmail').val();
        password = $('#pass').val();
        checkbox = $('#chkRememberMe').val();
    } else {
        username = '';
        password = '';
        checkbox = '';
    }
});