I extended the AspNetUser class with the LastLoginDate property to remember every time a user enters a login.
public sealed partial class User : IdentityUser<int, UserLogin, UserRole, UserClaim>, IUser<int>, IDatabaseEntity { ... public DateTime? LastLoginDate { get; set; } }
Logging stops every time a user enters a regular login using email and password as login credentials. In AccountController , I have an action to enter:
[HttpPost] public ActionResult Login(LoginModel model) { ... var result = SignInManager.PasswordSignIn(model.Email, model.Password, model.RememberMe); if (result == SignInStatus.Success) { var user = UserManager.FindByEmail(email); UserManager.RememberLoginDate(user); return Redirect(model.ReturnUrl); } ... }
RememberLoginDate are simply extension methods that basically just set the current time for the LastLoginDate custom property:
public static IdentityResult RememberLoginDate(this ApplicationUserManager manager, User user) { user.LastLoginDate = DateTime.UtcNow;; return manager.Update(user); }
But when I do an external login, as shown below:
[AllowAnonymous] public ActionResult ExternalLoginCallback(string returnUrl, string provider) { var loginInfo = AuthenticationManager.GetExternalLoginInfo(); if (loginInfo == null) return RedirectToAction("ExternalLoginFail");
How can I register the login date in this case? I do not have user information, such as email or Id , to get it using the UserManager . I already tried: User.Identity.GetUserId<int>() to get user Id after SignInManager.ExternalSignIn(loginInfo) , but it does not update after logging in, it still default(int) - 0 . SignInStatus is simply an enum without additional information about a registered user, and using ExternalLoginInfo.Email also not an option, because this e-mail may differ from the actual registered e-mail of the user.