-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathLoginAuditCreatedEventHandler.cs
More file actions
89 lines (73 loc) · 3.82 KB
/
LoginAuditCreatedEventHandler.cs
File metadata and controls
89 lines (73 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//------------------------------------------------------------------------------
// <auto-generated>
// CleanArchitecture.Blazor - MIT Licensed.
// Author: neozhu
// CreatedAt/Modified: 2025-03-19
// Handles LoginAuditCreatedEvent: triggered when a new contact is created.
// Extendable for additional actions (e.g., notifications, system updates).
// </auto-generated>
//------------------------------------------------------------------------------
using CleanArchitecture.Blazor.Application.Common.Interfaces;
using CleanArchitecture.Blazor.Domain.Enums;
using CleanArchitecture.Blazor.Domain.Identity;
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
using MaxMind.GeoIP2;
using Microsoft.Extensions.DependencyInjection;
using ZiggyCreatures.Caching.Fusion;
namespace CleanArchitecture.Blazor.Application.Features.LoginAudits.EventHandlers;
public class LoginAuditCreatedEventHandler : INotificationHandler<LoginAuditCreatedEvent>
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ILogger<LoginAuditCreatedEventHandler> _logger;
private readonly ISecurityAnalysisService _securityAnalysisService;
public LoginAuditCreatedEventHandler(
IServiceScopeFactory scopeFactory,
ILogger<LoginAuditCreatedEventHandler> logger,
ISecurityAnalysisService securityAnalysisService)
{
_scopeFactory = scopeFactory;
_logger = logger;
_securityAnalysisService = securityAnalysisService;
}
public async Task Handle(LoginAuditCreatedEvent notification, CancellationToken cancellationToken)
{
notification.Item.IpAddress = "186.150.138.239";
if (!string.IsNullOrEmpty(notification.Item.IpAddress) && !notification.Item.IpAddress.StartsWith("127") && string.IsNullOrEmpty(notification.Item.Region))
{
try
{
using (var scope = _scopeFactory.CreateScope())
{
using (var client = scope.ServiceProvider.GetRequiredService<WebServiceClient>())
{
var geolocation = await client.CityAsync(notification.Item.IpAddress);
if (geolocation != null)
{
var regionParts = new List<string>();
if (!string.IsNullOrEmpty(geolocation.City.Name))
regionParts.Add(geolocation.City.Name);
if (geolocation.Subdivisions.Any())
regionParts.Add(geolocation.Subdivisions.FirstOrDefault().Name);
if (!string.IsNullOrEmpty(geolocation.Country.Name))
regionParts.Add(geolocation.Country.Name);
var region = regionParts.Count > 0 ? string.Join(", ", regionParts) : null;
var dbContextFactory = scope.ServiceProvider.GetRequiredService<IApplicationDbContextFactory>();
await using var db = await dbContextFactory.CreateAsync(cancellationToken);
await db.LoginAudits.Where(x => x.Id == notification.Item.Id).ExecuteUpdateAsync(x => x.SetProperty(y => y.Region, region));
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to get geolocation for IP: {IpAddress}", notification.Item.IpAddress);
}
}
// Analyze account security using the dedicated service in a new scope
using (var scope = _scopeFactory.CreateScope())
{
var securityAnalysisService = scope.ServiceProvider.GetRequiredService<ISecurityAnalysisService>();
await securityAnalysisService.AnalyzeUserSecurityAsync(notification.Item, cancellationToken);
}
}
}