Files
shopadmin/ShopAdmin/Program.cs
T
Khwezi Mngoma 5f5f83a85a
continuous-integration/drone/pr Build is passing
Configured security, stable run
2026-05-17 08:29:12 +02:00

131 lines
4.2 KiB
C#

using LiteCharms.Features.Extensions;
using LiteCharms.Features.Mediator;
using Microsoft.AspNetCore.Authentication;
using ShopAdmin.Components;
using static LiteCharms.Features.Email.Extensions.Constants;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddCascadingAuthenticationState();
builder.AddMonitoring();
builder.Services.AddControllers();
builder.Services.AddBlazoredToast();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddMediator();
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(TelemetryPipelineBehavior<,>));
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>));
builder.Services.AddSalesServiceBus();
builder.Services.AddGeneralServiceBus();
builder.Services.AddQuartzSchedulerClient(ShopSchedulerName, builder.Configuration);
builder.Services.AddEmailServices(builder.Configuration);
builder.Services.AddEmailServiceBus();
builder.Services.AddShopServices();
builder.Services.AddShopDatabase(builder.Configuration);
builder.Services.AddPostgresHealtchCheck();
builder.Services.AddQuartzHealtchCheck();
builder.Services.AddHealthChecksSupport(builder.Configuration);
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = builder.Configuration.GetSection("IdKongisa:Authority").Value;
options.ClientId = builder.Configuration.GetSection("IdKongisa:ClientId").Value;
options.ClientSecret = builder.Configuration.GetSection("IdKongisa:ClientSecret").Value;
options.ResponseType = "code";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.MetadataAddress = $"{options.Authority}/.well-known/openid-configuration";
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "groups"
};
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProviderForSignOut = async callbackContext =>
{
var request = callbackContext.Request;
string currentBaseUrl = $"{request.Scheme}://{request.Host}{request.PathBase}/";
callbackContext.ProtocolMessage.PostLogoutRedirectUri = currentBaseUrl;
var idToken = await callbackContext.HttpContext.GetTokenAsync("id_token");
if (!string.IsNullOrEmpty(idToken)) callbackContext.ProtocolMessage.IdTokenHint = idToken;
}
};
});
var app = builder.Build();
var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>();
var scheduler = await schedulerFactory.GetScheduler(ShopSchedulerName);
if (!scheduler!.IsStarted)
await scheduler.Start();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = HealthChecks.UI.Client.UIResponseWriter.WriteHealthCheckUIResponse
});
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseHttpsRedirection();
app.UseAntiforgery();
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets();
app.MapGet("/auth/login", (string redirectUri = "/") =>
Results.Challenge(new AuthenticationProperties { RedirectUri = redirectUri }, [OpenIdConnectDefaults.AuthenticationScheme]));
app.MapGet("/auth/logout", async (HttpContext context) =>
{
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
string currentBaseUrl = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.PathBase}/";
await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties
{
RedirectUri = currentBaseUrl
});
});
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();