From d16cd86d942f6c86b6387893d0079c2c941109a6 Mon Sep 17 00:00:00 2001 From: hualin Date: Fri, 16 Aug 2024 13:39:11 +0800 Subject: [PATCH 1/4] improve loginfo --- .../ResetPassword/ResetPasswordCommand.cs | 4 +-- .../UserActivation/UserActivationCommand.cs | 4 +-- .../Authentication/ExternalLogin.razor | 2 +- .../Identity/Authentication/Register.razor | 6 ++++- .../Pages/Identity/Users/Users.razor | 27 ++++++++----------- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Application/Features/Identity/Notifications/ResetPassword/ResetPasswordCommand.cs b/src/Application/Features/Identity/Notifications/ResetPassword/ResetPasswordCommand.cs index 1c3e59431..6c9151c9a 100644 --- a/src/Application/Features/Identity/Notifications/ResetPassword/ResetPasswordCommand.cs +++ b/src/Application/Features/Identity/Notifications/ResetPassword/ResetPasswordCommand.cs @@ -36,7 +36,7 @@ public async Task Handle(ResetPasswordNotification notification, CancellationTok notification.UserName, notification.Email }); - _logger.LogInformation("Password rest email sent to {Email}. sending result {Successful} {ErrorMessages}", - notification.Email, sendMailResult.Successful, string.Join(' ', sendMailResult.ErrorMessages)); + _logger.LogInformation("Password rest email sent to {Email}. Reset Password Callback URL: {RequestUrl} sending result {Successful} {ErrorMessages}", + notification.Email, notification.RequestUrl, sendMailResult.Successful, string.Join(' ', sendMailResult.ErrorMessages)); } } \ No newline at end of file diff --git a/src/Application/Features/Identity/Notifications/UserActivation/UserActivationCommand.cs b/src/Application/Features/Identity/Notifications/UserActivation/UserActivationCommand.cs index 47fb6266d..dd8dba680 100644 --- a/src/Application/Features/Identity/Notifications/UserActivation/UserActivationCommand.cs +++ b/src/Application/Features/Identity/Notifications/UserActivation/UserActivationCommand.cs @@ -37,7 +37,7 @@ public async Task Handle(UserActivationNotification notification, CancellationTo notification.UserName, notification.Email }); - _logger.LogInformation("Activation email sent to {Email}. sending result {Successful} {Message}", - notification.Email, sendMailResult.Successful, string.Join(' ', sendMailResult.ErrorMessages)); + _logger.LogInformation("Activation email sent to {Email}, Activation Callback URL: {ActivationUrl}. sending result {Successful} {Message}, ", + notification.Email, notification.ActivationUrl,sendMailResult.Successful, string.Join(' ', sendMailResult.ErrorMessages)); } } \ No newline at end of file diff --git a/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor b/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor index 6f18743fd..8073ae578 100644 --- a/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor +++ b/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor @@ -137,7 +137,7 @@ if (result.Succeeded) { Logger.LogInformation( - "{Name} logged in with {LoginProvider} provider.", + "{UserName} logged in with {LoginProvider} provider.", externalLoginInfo.Principal.Identity?.Name, externalLoginInfo.LoginProvider); RedirectManager.RedirectTo(ReturnUrl); diff --git a/src/Server.UI/Pages/Identity/Authentication/Register.razor b/src/Server.UI/Pages/Identity/Authentication/Register.razor index 87538d277..1a0fe2543 100644 --- a/src/Server.UI/Pages/Identity/Authentication/Register.razor +++ b/src/Server.UI/Pages/Identity/Authentication/Register.razor @@ -188,7 +188,11 @@ } var userId = await UserManager.GetUserIdAsync(user); - Logger.LogInformation("{UserName} created a new account with password.", Input.UserName); + Logger.LogInformation("New user account created. Username: {UserName}, User ID: {UserId}, Tenant ID: {TenantId}, Assigned Role: {RoleName}.", + Input.UserName, + userId, + Input.TenantId, + RoleName.Basic); if (UserManager.Options.SignIn.RequireConfirmedEmail) { var code = await UserManager.GenerateEmailConfirmationTokenAsync(user); diff --git a/src/Server.UI/Pages/Identity/Users/Users.razor b/src/Server.UI/Pages/Identity/Users/Users.razor index 28155fb6b..768243cc0 100644 --- a/src/Server.UI/Pages/Identity/Users/Users.razor +++ b/src/Server.UI/Pages/Identity/Users/Users.razor @@ -152,7 +152,7 @@ @if (_canSearch) { - + @foreach (var str in _roles) { @str @@ -223,7 +223,7 @@ -
+
@if (string.IsNullOrEmpty(context.Item.ProfilePictureDataUrl)) @@ -286,18 +286,19 @@
- @if (!context.Item.IsActive) + @if (!context.Item.IsActive || (context.Item.LockoutEnd is not null && context.Item.LockoutEnd > DateTime.UtcNow)) { - - + + } else { - - + + + }
@@ -351,7 +352,6 @@ private bool _canManagePermissions; private bool _canImport; private bool _canExport; - private bool _canDeactivation; private bool _loading; private bool _exporting; private bool _uploading; @@ -373,7 +373,6 @@ _canManagePermissions = (await AuthService.AuthorizeAsync(state.User, Permissions.Users.ManagePermissions)).Succeeded; _canImport = (await AuthService.AuthorizeAsync(state.User, Permissions.Users.Import)).Succeeded; _canExport = (await AuthService.AuthorizeAsync(state.User, Permissions.Users.Export)).Succeeded; - _canDeactivation = (await AuthService.AuthorizeAsync(state.User, Permissions.Users.Deactivation)).Succeeded; _roles = await RoleManager.Roles.Select(x => x.Name).ToListAsync(); @@ -521,14 +520,9 @@ user.DisplayName = item.DisplayName; user.Provider = item.Provider; user.UserName = item.UserName; - + user.IsActive = item.IsActive; user.TenantId = item.TenantId; user.SuperiorId = item.SuperiorId; - if (user.IsActive == false && item.IsActive && user.LockoutEnd is not null) - { - user.LockoutEnd = null; - } - user.IsActive = item.IsActive; var identityResult = await UserManager.UpdateAsync(user); if (identityResult.Succeeded) { @@ -702,11 +696,13 @@ private async Task DeactivateUser(ApplicationUser user, ApplicationUserDto item) { user.IsActive = false; + user.LockoutEnd = DateTimeOffset.MaxValue; var identityResult = await UserManager.UpdateAsync(user); if (identityResult.Succeeded) { item.IsActive = false; + item.LockoutEnd = DateTimeOffset.MaxValue; Snackbar.Add($"{L["The user has been inactivated."]}", Severity.Info); } else @@ -725,7 +721,6 @@ new Dictionary { ["userId"] = user.Id, ["token"] = code }); await Mediator.Publish(new ResetPasswordNotification(callbackUrl, item.Email, item.UserName)); Snackbar.Add(string.Format(L["A new password for {0} has been sent via email. The user will be required to enter a new password upon initial login."], item.UserName), Severity.Info); - Logger.LogInformation($"Password reset email sent to user {item.UserName} ({item.Id}) with callback URL {callbackUrl}"); } private async Task OnResetPassword(ApplicationUserDto item) { From c4d02f2557264f5f6f0b6e652e902ac1934dd3cb Mon Sep 17 00:00:00 2001 From: hualin Date: Fri, 16 Aug 2024 13:42:01 +0800 Subject: [PATCH 2/4] Correct spelling --- src/Server.UI/Pages/SystemManagement/AuditTrails.razor | 2 +- src/Server.UI/Pages/SystemManagement/Dictionaries.razor | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server.UI/Pages/SystemManagement/AuditTrails.razor b/src/Server.UI/Pages/SystemManagement/AuditTrails.razor index a38885c63..9f50694a0 100644 --- a/src/Server.UI/Pages/SystemManagement/AuditTrails.razor +++ b/src/Server.UI/Pages/SystemManagement/AuditTrails.razor @@ -42,7 +42,7 @@
- + diff --git a/src/Server.UI/Pages/SystemManagement/Dictionaries.razor b/src/Server.UI/Pages/SystemManagement/Dictionaries.razor index 8cc75fefb..f47e6bd30 100644 --- a/src/Server.UI/Pages/SystemManagement/Dictionaries.razor +++ b/src/Server.UI/Pages/SystemManagement/Dictionaries.razor @@ -141,7 +141,7 @@ @if (_canSearch) { - + From cfe301386baccbdfc46194f4a96bea8e148b9fcd Mon Sep 17 00:00:00 2001 From: hualin Date: Fri, 16 Aug 2024 13:43:39 +0800 Subject: [PATCH 3/4] Update SendFactorCodeCommand.cs --- .../Notifications/SendFactorCode/SendFactorCodeCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Application/Features/Identity/Notifications/SendFactorCode/SendFactorCodeCommand.cs b/src/Application/Features/Identity/Notifications/SendFactorCode/SendFactorCodeCommand.cs index 283577d9a..8ca7709f5 100644 --- a/src/Application/Features/Identity/Notifications/SendFactorCode/SendFactorCodeCommand.cs +++ b/src/Application/Features/Identity/Notifications/SendFactorCode/SendFactorCodeCommand.cs @@ -35,7 +35,7 @@ public async Task Handle(SendFactorCodeNotification notification, CancellationTo notification.AuthenticatorCode, _settings.AppName, notification.Email, notification.UserName, _settings.Company }); - _logger.LogInformation("Verification Code email sent to {Email}. sending result {Successful} {ErrorMessages}", - notification.Email, sendMailResult.Successful, string.Join(' ', sendMailResult.ErrorMessages)); + _logger.LogInformation("Verification Code email sent to {Email}. Authenticator Code:{AuthenticatorCode} sending result {Successful} {ErrorMessages}", + notification.Email, notification.AuthenticatorCode,sendMailResult.Successful, string.Join(' ', sendMailResult.ErrorMessages)); } } \ No newline at end of file From 91b234058c5454c4f6538b3a12855782d2524426 Mon Sep 17 00:00:00 2001 From: hualin Date: Fri, 16 Aug 2024 13:52:00 +0800 Subject: [PATCH 4/4] Update ExternalLogin.razor --- .../Identity/Authentication/ExternalLogin.razor | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor b/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor index 8073ae578..db6d40847 100644 --- a/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor +++ b/src/Server.UI/Pages/Identity/Authentication/ExternalLogin.razor @@ -136,14 +136,16 @@ if (result.Succeeded) { - Logger.LogInformation( - "{UserName} logged in with {LoginProvider} provider.", - externalLoginInfo.Principal.Identity?.Name, + Logger.LogInformation("{UserName} successfully logged in using {LoginProvider} external provider.", + externalLoginInfo.Principal.Identity?.Name ?? "Unknown User", externalLoginInfo.LoginProvider); RedirectManager.RedirectTo(ReturnUrl); } else if (result.IsLockedOut) { + Logger.LogWarning("{UserName} is locked out during login attempt with {LoginProvider} external provider.", + externalLoginInfo.Principal.Identity?.Name ?? "Unknown User", + externalLoginInfo.LoginProvider); RedirectManager.RedirectTo(Lockout.PageUrl); } @@ -184,7 +186,9 @@ return; } - Logger.LogInformation("{UserName} created an account using {Name} provider.", Input.Email, externalLoginInfo.LoginProvider); + Logger.LogInformation("A new account was created for user '{UserName}' using the external login provider '{LoginProvider}'.", + Input.Email, + externalLoginInfo.LoginProvider); if (UserManager.Options.SignIn.RequireConfirmedEmail) {