如何在Owin上下文中管理用户登录历史记录(在ASP.NET WebAPI中)

时间:2016-09-15 06:51:33

标签: c# asp.net asp.net-web-api

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
        ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return;
        }

        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);
        user.Logins.Add(new IdentityUserLogin { UserId = user.Id, LoginProvider = "self",ProviderKey="" });

        await userManager.UpdateAsync(user);

    }

这是我的代码完成。现在我想管理用户的登录历史记录,如LastLoginDate这是我的代码完成。

0 个答案:

没有答案