From bcfc9414d3b5df1641fac463e9c5f609291f64a7 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 20 Aug 2020 13:19:27 +0200 Subject: [PATCH] Update user on successfully login --- .../Security/BackOfficeSignInManager.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web.Common/Security/BackOfficeSignInManager.cs b/src/Umbraco.Web.Common/Security/BackOfficeSignInManager.cs index b39e54935d..07f7470243 100644 --- a/src/Umbraco.Web.Common/Security/BackOfficeSignInManager.cs +++ b/src/Umbraco.Web.Common/Security/BackOfficeSignInManager.cs @@ -45,7 +45,7 @@ namespace Umbraco.Web.Common.Security { // override to handle logging/events var result = await base.PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure); - return HandlePasswordSignIn(user, user.UserName, result); + return await HandlePasswordSignIn(user, user.UserName, result); } public override async Task PasswordSignInAsync(string userName, string password, bool isPersistent, bool lockoutOnFailure) @@ -53,7 +53,7 @@ namespace Umbraco.Web.Common.Security // override to handle logging/events var user = await UserManager.FindByNameAsync(userName); if (user == null) - return HandlePasswordSignIn(null, userName, SignInResult.Failed); + return await HandlePasswordSignIn(null, userName, SignInResult.Failed); return await PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure); } @@ -62,7 +62,7 @@ namespace Umbraco.Web.Common.Security // override to handle logging/events var result = await base.TwoFactorSignInAsync(provider, code, isPersistent, rememberClient); var user = await GetTwoFactorAuthenticationUserAsync(); // will never be null if the above succeeds - return HandlePasswordSignIn(user, user?.UserName, result); + return await HandlePasswordSignIn(user, user?.UserName, result); } public override bool IsSignedIn(ClaimsPrincipal principal) @@ -125,13 +125,20 @@ namespace Umbraco.Web.Common.Security //await Context.SignOutAsync(IdentityConstants.TwoFactorUserIdScheme); } - private SignInResult HandlePasswordSignIn(BackOfficeIdentityUser user, string username, SignInResult result) + private async Task HandlePasswordSignIn(BackOfficeIdentityUser user, string username, SignInResult result) { if (username.IsNullOrWhiteSpace()) username = "UNKNOWN"; // could happen in 2fa or something else weird if (result.Succeeded) { + //track the last login date + user.LastLoginDateUtc = DateTime.UtcNow; + if (user.AccessFailedCount > 0) + //we have successfully logged in, reset the AccessFailedCount + user.AccessFailedCount = 0; + await _userManager.UpdateAsync(user); + Logger.LogInformation("User: {UserName} logged in from IP address {IpAddress}", username, Context.Connection.RemoteIpAddress); if (user != null) _userManager.RaiseLoginSuccessEvent(user, user.Id);