mirror of https://github.com/abpframework/abp.git
15 changed files with 271 additions and 51 deletions
@ -1,11 +1,10 @@ |
|||||
using System.Threading.Tasks; |
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
namespace Volo.Abp.SecurityLog |
namespace Volo.Abp.SecurityLog |
||||
{ |
{ |
||||
public interface ISecurityLogManager |
public interface ISecurityLogManager |
||||
{ |
{ |
||||
Task<SecurityLogInfo> CreateAsync(); |
Task SaveAsync(Action<SecurityLogInfo> saveAction); |
||||
|
|
||||
Task SaveAsync(SecurityLogInfo securityLogInfo); |
|
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,37 @@ |
|||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
|
||||
|
namespace Volo.Abp.Identity.AspNetCore |
||||
|
{ |
||||
|
public static class SignInResultExtensions |
||||
|
{ |
||||
|
public static string ToIdentitySecurityLogAction(this SignInResult result) |
||||
|
{ |
||||
|
if (result.Succeeded) |
||||
|
{ |
||||
|
return IdentitySecurityLogActionConsts.LoginSucceeded; |
||||
|
} |
||||
|
|
||||
|
if (result.IsLockedOut) |
||||
|
{ |
||||
|
return IdentitySecurityLogActionConsts.LoginLockedout; |
||||
|
} |
||||
|
|
||||
|
if (result.RequiresTwoFactor) |
||||
|
{ |
||||
|
return IdentitySecurityLogActionConsts.LoginRequiresTwoFactor; |
||||
|
} |
||||
|
|
||||
|
if (result.IsNotAllowed) |
||||
|
{ |
||||
|
return IdentitySecurityLogActionConsts.LoginNotAllowed; |
||||
|
} |
||||
|
|
||||
|
if (!result.Succeeded) |
||||
|
{ |
||||
|
return IdentitySecurityLogActionConsts.LoginFailed; |
||||
|
} |
||||
|
|
||||
|
return IdentitySecurityLogActionConsts.LoginFailed; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,33 @@ |
|||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public class IdentitySecurityLogActionConsts |
||||
|
{ |
||||
|
public static string LoginSucceeded { get; set; } = "LoginSucceeded"; |
||||
|
|
||||
|
public static string LoginLockedout { get; set; } = "LoginLockedout"; |
||||
|
|
||||
|
public static string LoginNotAllowed { get; set; } = "LoginNotAllowed"; |
||||
|
|
||||
|
public static string LoginRequiresTwoFactor { get; set; } = "LoginRequiresTwoFactor"; |
||||
|
|
||||
|
public static string LoginFailed { get; set; } = "LoginFailed"; |
||||
|
|
||||
|
public static string LoginInvalidUserName { get; set; } = "LoginInvalidUserName"; |
||||
|
|
||||
|
public static string LoginInvalidUserNameOrPassword { get; set; } = "LoginInvalidUserNameOrPassword"; |
||||
|
|
||||
|
public static string Logout { get; set; } = "Logout"; |
||||
|
|
||||
|
public static string ChangeUserName { get; set; } = "ChangeUserName"; |
||||
|
|
||||
|
public static string ChangeEmail { get; set; } = "ChangeEmail"; |
||||
|
|
||||
|
public static string ChangePhoneNumber { get; set; } = "ChangePhoneNumber"; |
||||
|
|
||||
|
public static string ChangePassword { get; set; } = "ChangePassword"; |
||||
|
|
||||
|
public static string TwoFactorEnabled { get; set; } = "TwoFactorEnabled"; |
||||
|
|
||||
|
public static string TwoFactorDisabled { get; set; } = "TwoFactorDisabled"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public static class IdentitySecurityLogIdentityConsts |
||||
|
{ |
||||
|
public static string Identity { get; set; } = "Identity"; |
||||
|
|
||||
|
public static string IdentityExternal { get; set; } = "IdentityExternal"; |
||||
|
|
||||
|
public static string IdentityTwoFactor { get; set; } = "IdentityTwoFactor"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
|
||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public class SecurityLogEvent : IMultiTenant |
||||
|
{ |
||||
|
public Guid? TenantId { get; set; } |
||||
|
|
||||
|
public string Identity { get; set; } |
||||
|
|
||||
|
public string Action { get; set; } |
||||
|
|
||||
|
public string UserName { get; set; } |
||||
|
|
||||
|
public string ClientId { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,85 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.EventBus; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
using Volo.Abp.SecurityLog; |
||||
|
using Volo.Abp.Uow; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public class SecurityLogHandler : ILocalEventHandler<SecurityLogEvent>, ITransientDependency |
||||
|
{ |
||||
|
protected ISecurityLogManager SecurityLogManager { get; } |
||||
|
protected IdentityUserManager UserManager { get; } |
||||
|
protected ICurrentPrincipalAccessor CurrentPrincipalAccessor { get; } |
||||
|
protected IUserClaimsPrincipalFactory<IdentityUser> UserClaimsPrincipalFactory { get; } |
||||
|
protected ICurrentUser CurrentUser { get; } |
||||
|
protected IUnitOfWorkManager UnitOfWorkManager { get; } |
||||
|
|
||||
|
public SecurityLogHandler( |
||||
|
ISecurityLogManager securityLogManager, |
||||
|
IdentityUserManager userManager, |
||||
|
ICurrentPrincipalAccessor currentPrincipalAccessor, |
||||
|
IUserClaimsPrincipalFactory<IdentityUser> userClaimsPrincipalFactory, |
||||
|
ICurrentUser currentUser, |
||||
|
IUnitOfWorkManager unitOfWorkManager) |
||||
|
{ |
||||
|
SecurityLogManager = securityLogManager; |
||||
|
UserManager = userManager; |
||||
|
CurrentPrincipalAccessor = currentPrincipalAccessor; |
||||
|
UserClaimsPrincipalFactory = userClaimsPrincipalFactory; |
||||
|
CurrentUser = currentUser; |
||||
|
UnitOfWorkManager = unitOfWorkManager; |
||||
|
} |
||||
|
|
||||
|
public async Task HandleEventAsync(SecurityLogEvent eventData) |
||||
|
{ |
||||
|
Action<SecurityLogInfo> securityLogAction = securityLog => |
||||
|
{ |
||||
|
securityLog.Identity = eventData.Identity; |
||||
|
securityLog.Action = eventData.Action; |
||||
|
|
||||
|
if (securityLog.UserName.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
securityLog.UserName = eventData.UserName; |
||||
|
} |
||||
|
|
||||
|
if (securityLog.ClientId.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
securityLog.ClientId = eventData.ClientId; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
using (var uow = UnitOfWorkManager.Begin(requiresNew: true)) |
||||
|
{ |
||||
|
if (CurrentUser.IsAuthenticated) |
||||
|
{ |
||||
|
await SecurityLogManager.SaveAsync(securityLogAction); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
if (eventData.UserName.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
await SecurityLogManager.SaveAsync(securityLogAction); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
var user = await UserManager.FindByNameAsync(eventData.UserName); |
||||
|
if (user != null) |
||||
|
{ |
||||
|
using (CurrentPrincipalAccessor.Change(await UserClaimsPrincipalFactory.CreateAsync(user))) |
||||
|
{ |
||||
|
await SecurityLogManager.SaveAsync(securityLogAction); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
await uow.CompleteAsync(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue