chore: ADConfigurationOptions mit appsettings verknüpfen
This commit is contained in:
parent
6a4f8a12c7
commit
6bc96205ce
@ -1,3 +1,4 @@
|
||||
using DigitalData.ActiveDirectory;
|
||||
using DigitalData.ActiveDirectory.API.Middleware;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@ -9,6 +10,12 @@ builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddActiveDirectory(options =>
|
||||
{
|
||||
options.ConfigActiveDirectory(builder.Configuration.GetSection("ActiveDirectory"));
|
||||
options.MediatRLicenseKey = builder.Configuration["MediatRLicense"];
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
|
||||
@ -5,5 +5,11 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ActiveDirectory": {
|
||||
"Path": "LDAP://DD-VMP01-DC01/DC=dd-gan,DC=local,DC=digitaldata,DC=works",
|
||||
"Username": "FABRIK19-User01",
|
||||
"Password": "9bWOr0UGuHn_7VkC"
|
||||
},
|
||||
"MediatRLicense": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ikx1Y2t5UGVubnlTb2Z0d2FyZUxpY2Vuc2VLZXkvYmJiMTNhY2I1OTkwNGQ4OWI0Y2IxYzg1ZjA4OGNjZjkiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2x1Y2t5cGVubnlzb2Z0d2FyZS5jb20iLCJhdWQiOiJMdWNreVBlbm55U29mdHdhcmUiLCJleHAiOiIxNzg0ODUxMjAwIiwiaWF0IjoiMTc1MzM2MjQ5MSIsImFjY291bnRfaWQiOiIwMTk4M2M1OWU0YjM3MjhlYmZkMzEwM2MyYTQ4NmU4NSIsImN1c3RvbWVyX2lkIjoiY3RtXzAxazB5NmV3MmQ4YTk4Mzg3aDJnbTRuOWswIiwic3ViX2lkIjoiLSIsImVkaXRpb24iOiIwIiwidHlwZSI6IjIifQ.ZqsFG7kv_-xGfxS6ACk3i0iuNiVUXX2AvPI8iAcZ6-z2170lGv__aO32tWpQccD9LCv5931lBNLWSblKS0MT3gOt-5he2TEftwiSQGFwoIBgtOHWsNRMinUrg2trceSp3IhyS3UaMwnxZDrCvx4-0O-kpOzVpizeHUAZNr5U7oSCWO34bpKdae6grtM5e3f93Z1vs7BW_iPgItd-aLvPwApbaG9VhmBTKlQ7b4Jh64y7UXJ9mKP7Qb_Oa97oEg0oY5DPHOWTZWeE1EzORgVr2qkK2DELSHuZ_EIUhODojkClPNAKtvEl_qEjpq0HZCIvGwfCCRlKlSkQqIeZdFkiXg"
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddActiveDirectory(this IServiceCollection services, Action<ADConfigurationOptions>? options = null)
|
||||
{
|
||||
var cOptions = new ADConfigurationOptions();
|
||||
var cOptions = new ADConfigurationOptions(services);
|
||||
options?.Invoke(cOptions);
|
||||
|
||||
services.AddMediatR(cfg =>
|
||||
@ -16,20 +16,32 @@ public static class DependencyInjection
|
||||
cfg.LicenseKey = cOptions.MediatRLicenseKey;
|
||||
});
|
||||
|
||||
if (cOptions.Configuration is null)
|
||||
services.Configure<ActiveDirectoryOptions>(o => o = cOptions);
|
||||
else
|
||||
services.Configure<ActiveDirectoryOptions>(cOptions.Configuration);
|
||||
if (!cOptions.IsADConfigured)
|
||||
services.Configure<ActiveDirectoryOptions>(_ => { });
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public class ADConfigurationOptions : ActiveDirectoryOptions
|
||||
{
|
||||
internal ADConfigurationOptions() { }
|
||||
private readonly IServiceCollection _services;
|
||||
|
||||
internal bool IsADConfigured { get; private set; } = false;
|
||||
|
||||
internal ADConfigurationOptions(IServiceCollection services) => _services = services;
|
||||
|
||||
public string? MediatRLicenseKey { get; set; }
|
||||
|
||||
public IConfiguration? Configuration { get; set; }
|
||||
private void EnsureSingleMappingConfiguration(Action action)
|
||||
{
|
||||
if (IsADConfigured)
|
||||
throw new InvalidOperationException("Mapping configuration has already been set.");
|
||||
action();
|
||||
IsADConfigured = true;
|
||||
}
|
||||
|
||||
public void ConfigActiveDirectory(IConfiguration config) => EnsureSingleMappingConfiguration(() => _services.Configure<ActiveDirectoryOptions>(config));
|
||||
|
||||
public void ConfigActiveDirectory(Action<ActiveDirectoryOptions> options) => EnsureSingleMappingConfiguration(() => _services.Configure(options));
|
||||
}
|
||||
}
|
||||
@ -12,14 +12,20 @@ public record DirectorySearchQuery(string? Filter = null, SearchScope Scope = Se
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
public class DirectorySearchQueryHandler(IOptions<ActiveDirectoryOptions> options) : IRequestHandler<DirectorySearchQuery, IEnumerable<ResultPropertyCollection>>
|
||||
public class DirectorySearchQueryHandler : IRequestHandler<DirectorySearchQuery, IEnumerable<ResultPropertyCollection>>
|
||||
{
|
||||
|
||||
private readonly IOptions<ActiveDirectoryOptions> _options;
|
||||
|
||||
public DirectorySearchQueryHandler(IOptions<ActiveDirectoryOptions> options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public DirectoryEntry DirectoryEntry => new ()
|
||||
{
|
||||
Path = options.Value.Path,
|
||||
Username = options.Value.Username,
|
||||
Password = options.Value.Password
|
||||
Path = _options.Value.Path,
|
||||
Username = _options.Value.Username,
|
||||
Password = _options.Value.Password
|
||||
};
|
||||
|
||||
public Task<IEnumerable<ResultPropertyCollection>> Handle(DirectorySearchQuery request, CancellationToken cancellationToken = default)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user