mirror of https://github.com/abpframework/abp.git
15 changed files with 277 additions and 37 deletions
@ -0,0 +1,74 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.AspNetCore.WebClientInfo; |
||||
|
using Volo.Abp.Clients; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.SecurityLog; |
||||
|
using Volo.Abp.Timing; |
||||
|
using Volo.Abp.Tracing; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.SecurityLog |
||||
|
{ |
||||
|
[Dependency(ReplaceServices = true)] |
||||
|
public class AspNetCoreSecurityLogManager : DefaultSecurityLogManager |
||||
|
{ |
||||
|
protected ILogger<AspNetCoreSecurityLogManager> Logger { get; } |
||||
|
protected IClock Clock { get; } |
||||
|
protected ICurrentUser CurrentUser { get; } |
||||
|
protected ICurrentTenant CurrentTenant { get; } |
||||
|
protected ICurrentClient CurrentClient { get; } |
||||
|
protected IHttpContextAccessor HttpContextAccessor { get; } |
||||
|
protected ICorrelationIdProvider CorrelationIdProvider { get; } |
||||
|
|
||||
|
protected IWebClientInfoProvider WebClientInfoProvider { get; } |
||||
|
|
||||
|
public AspNetCoreSecurityLogManager( |
||||
|
IOptions<AbpSecurityLogOptions> securityLogOptions, |
||||
|
ISecurityLogStore securityLogStore, |
||||
|
ILogger<AspNetCoreSecurityLogManager> logger, |
||||
|
IClock clock, |
||||
|
ICurrentUser currentUser, |
||||
|
ICurrentTenant currentTenant, |
||||
|
ICurrentClient currentClient, |
||||
|
IHttpContextAccessor httpContextAccessor, |
||||
|
ICorrelationIdProvider correlationIdProvider, |
||||
|
IWebClientInfoProvider webClientInfoProvider) |
||||
|
: base(securityLogOptions, securityLogStore) |
||||
|
{ |
||||
|
Logger = logger; |
||||
|
Clock = clock; |
||||
|
CurrentUser = currentUser; |
||||
|
CurrentTenant = currentTenant; |
||||
|
CurrentClient = currentClient; |
||||
|
HttpContextAccessor = httpContextAccessor; |
||||
|
CorrelationIdProvider = correlationIdProvider; |
||||
|
WebClientInfoProvider = webClientInfoProvider; |
||||
|
} |
||||
|
|
||||
|
public override async Task<SecurityLogInfo> CreateAsync() |
||||
|
{ |
||||
|
var securityLogInfo = await base.CreateAsync(); |
||||
|
|
||||
|
securityLogInfo.CreationTime = Clock.Now; |
||||
|
|
||||
|
securityLogInfo.TenantId = CurrentTenant.Id; |
||||
|
securityLogInfo.TenantName = CurrentTenant.Name; |
||||
|
|
||||
|
securityLogInfo.UserId = CurrentUser.Id; |
||||
|
securityLogInfo.UserName = CurrentUser.UserName; |
||||
|
|
||||
|
securityLogInfo.ClientId = CurrentClient.Id; |
||||
|
|
||||
|
securityLogInfo.CorrelationId = CorrelationIdProvider.Get(); |
||||
|
|
||||
|
securityLogInfo.ClientIpAddress = WebClientInfoProvider.ClientIpAddress; |
||||
|
securityLogInfo.BrowserInfo = WebClientInfoProvider.BrowserInfo; |
||||
|
|
||||
|
return securityLogInfo; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
using System; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.WebClientInfo |
||||
|
{ |
||||
|
public class HttpContextWebClientInfoProvider : IWebClientInfoProvider, ITransientDependency |
||||
|
{ |
||||
|
protected ILogger<HttpContextWebClientInfoProvider> Logger { get; } |
||||
|
protected IHttpContextAccessor HttpContextAccessor { get; } |
||||
|
|
||||
|
public HttpContextWebClientInfoProvider( |
||||
|
ILogger<HttpContextWebClientInfoProvider> logger, |
||||
|
IHttpContextAccessor httpContextAccessor) |
||||
|
{ |
||||
|
Logger = logger; |
||||
|
HttpContextAccessor = httpContextAccessor; |
||||
|
} |
||||
|
|
||||
|
public string BrowserInfo => GetBrowserInfo(); |
||||
|
|
||||
|
public string ClientIpAddress => GetClientIpAddress(); |
||||
|
|
||||
|
protected virtual string GetBrowserInfo() |
||||
|
{ |
||||
|
return HttpContextAccessor.HttpContext?.Request?.Headers?["User-Agent"]; |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetClientIpAddress() |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
return HttpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString(); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
Logger.LogException(ex, LogLevel.Warning); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.AspNetCore.WebClientInfo |
||||
|
{ |
||||
|
public interface IWebClientInfoProvider |
||||
|
{ |
||||
|
string BrowserInfo { get; } |
||||
|
|
||||
|
string ClientIpAddress { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
namespace Volo.Abp.SecurityLog |
||||
|
{ |
||||
|
public class AbpSecurityLogOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Default: true.
|
||||
|
/// </summary>
|
||||
|
public bool IsEnabled { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The name of the application or service writing security log.
|
||||
|
/// Default: null.
|
||||
|
/// </summary>
|
||||
|
public string ApplicationName { get; set; } |
||||
|
|
||||
|
public AbpSecurityLogOptions() |
||||
|
{ |
||||
|
IsEnabled = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.SecurityLog |
||||
|
{ |
||||
|
public class DefaultSecurityLogManager : ISecurityLogManager, ITransientDependency |
||||
|
{ |
||||
|
protected AbpSecurityLogOptions SecurityLogOptions { get; } |
||||
|
|
||||
|
protected ISecurityLogStore SecurityLogStore { get; } |
||||
|
|
||||
|
public DefaultSecurityLogManager( |
||||
|
IOptions<AbpSecurityLogOptions> securityLogOptions, |
||||
|
ISecurityLogStore securityLogStore) |
||||
|
{ |
||||
|
SecurityLogStore = securityLogStore; |
||||
|
SecurityLogOptions = securityLogOptions.Value; |
||||
|
} |
||||
|
|
||||
|
public virtual Task<SecurityLogInfo> CreateAsync() |
||||
|
{ |
||||
|
return Task.FromResult(new SecurityLogInfo |
||||
|
{ |
||||
|
ApplicationName = SecurityLogOptions.ApplicationName |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public async Task SaveAsync(SecurityLogInfo securityLogInfo) |
||||
|
{ |
||||
|
await SecurityLogStore.SaveAsync(securityLogInfo); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.SecurityLog |
||||
|
{ |
||||
|
public interface ISecurityLogManager |
||||
|
{ |
||||
|
Task<SecurityLogInfo> CreateAsync(); |
||||
|
|
||||
|
Task SaveAsync(SecurityLogInfo securityLogInfo); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.SecurityLog |
||||
|
{ |
||||
|
public interface ISecurityLogStore |
||||
|
{ |
||||
|
Task SaveAsync(SecurityLogInfo securityLogInfo); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.SecurityLog |
||||
|
{ |
||||
|
public class SimpleSecurityLogStore : ISecurityLogStore, ITransientDependency |
||||
|
{ |
||||
|
public ILogger<SimpleSecurityLogStore> Logger { get; set; } |
||||
|
|
||||
|
public SimpleSecurityLogStore(ILogger<SimpleSecurityLogStore> logger) |
||||
|
{ |
||||
|
Logger = logger; |
||||
|
} |
||||
|
|
||||
|
public Task SaveAsync(SecurityLogInfo securityLogInfo) |
||||
|
{ |
||||
|
Logger.LogInformation(securityLogInfo.ToString()); |
||||
|
return Task.FromResult(0); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,9 +0,0 @@ |
|||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Volo.Abp.Users.SecurityLog |
|
||||
{ |
|
||||
public interface IUserSecurityLogStore |
|
||||
{ |
|
||||
Task SaveAsync(UserSecurityLogInfo userSecurityLogInfo); |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue