Add current user service and use for catalog audit fields
Introduce ICurrentUserService and its implementation to access the current user's username. Inject this service into CreateCatalogHandler and UpdateCatalogHandler to set AddedWho and ChangedWho fields dynamically. Register the service and IHttpContextAccessor in DI, and enable authentication middleware. Update project and using statements accordingly.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using DbFirst.API.Dashboards;
|
||||
using DbFirst.API.Hubs;
|
||||
using DbFirst.API.Middleware;
|
||||
using DbFirst.API.Services;
|
||||
using DbFirst.Application;
|
||||
using DbFirst.Application.Abstractions;
|
||||
using DbFirst.Infrastructure;
|
||||
using DevExpress.AspNetCore;
|
||||
using DevExpress.DashboardAspNetCore;
|
||||
@@ -51,6 +53,9 @@ builder.Services.AddSingleton<IDashboardChangeNotifier, DashboardChangeNotifier>
|
||||
builder.Services.AddScoped<DashboardConfigurator>(sp =>
|
||||
DashboardConfiguratorFactory.Create(sp, builder.Configuration, builder.Environment));
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -65,6 +70,7 @@ app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
app.UseDevExpressControls();
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapDashboardRoute("api/dashboard", "DefaultDashboard");
|
||||
|
||||
10
DbFirst.API/Services/CurrentUserService.cs
Normal file
10
DbFirst.API/Services/CurrentUserService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using DbFirst.Application.Abstractions;
|
||||
|
||||
namespace DbFirst.API.Services
|
||||
{
|
||||
public class CurrentUserService(IHttpContextAccessor httpContextAccessor) : ICurrentUserService
|
||||
{
|
||||
public string UserName =>
|
||||
httpContextAccessor.HttpContext?.User.Identity?.Name ?? "unknown";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user