diff --git a/src/Server/Program.cs b/src/Server/Program.cs index 2dadd78..df02526 100644 --- a/src/Server/Program.cs +++ b/src/Server/Program.cs @@ -1,15 +1,33 @@ using ElmahCore; using ElmahCore.Mvc; using Serilog; +using System.Globalization; +using Microsoft.AspNetCore.Localization; using Server; using Server.HealthChecks; using Server.Helper; +using Server.Models; +using Server.Services; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); + +// Add Localization +builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); +builder.Services.Configure(options => +{ + var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("de") }; + options.DefaultRequestCulture = new RequestCulture("en"); + options.SupportedCultures = supportedCultures; + options.SupportedUICultures = supportedCultures; +}); + +// Add LocalizationService +builder.Services.AddScoped(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); @@ -30,6 +48,24 @@ builder.Services.AddElmah(Options => Options.LogPath = builder.Configuration.GetValue("Embeddingsearch:Elmah:LogFolder") ?? "~/logs"; }); +builder.Services + .AddAuthentication("AppCookie") + .AddCookie("AppCookie", options => + { + options.LoginPath = "/Account/Login"; + options.LogoutPath = "/Account/Logout"; + options.AccessDeniedPath = "/Account/Denied"; + }); + +builder.Services.AddAuthorization(options => +{ + options.AddPolicy("AdminOnly", + policy => policy.RequireRole("Admin")); +}); + +IConfigurationSection simpleAuthSection = builder.Configuration.GetSection("Embeddingsearch:SimpleAuth"); +if (simpleAuthSection.Exists()) builder.Services.Configure(simpleAuthSection); + var app = builder.Build(); List? allowedIps = builder.Configuration.GetSection("Embeddingsearch:Elmah:AllowedHosts") @@ -77,6 +113,15 @@ if (UseMiddleware == true && !IsDevelopment) app.UseMiddleware(); } +// Add localization +var supportedCultures = new[] { "de", "de-DE", "en-US" }; +var localizationOptions = new RequestLocalizationOptions() + .SetDefaultCulture("de") + .AddSupportedCultures(supportedCultures) + .AddSupportedUICultures(supportedCultures); +app.UseRequestLocalization(localizationOptions); + +app.UseAuthentication(); app.UseAuthorization(); app.MapControllers();