Files
NTBug/web/Helpdesk.Web/Program.cs
T
PatTheEvil abd075287c web: založení projektu Helpdesk.Web a layout aplikace
ASP.NET Core MVC (.NET 10) s Bootstrapem 5, layout s menu podle role,
chybové stránky, bezpečnostní hlavičky (CSP, Referrer-Policy) a česká kultura.

Refs #19, #20
2026-12-02 00:20:49 +01:00

71 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using System.Net;
using Helpdesk.Web.Infrastructure;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews(options =>
{
// Anti-forgery token u všech formulářů odesílaných metodou POST (CSRF, kap. 10).
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
});
// Klíče Data Protection šifrují autentizační cookie. Bez uložení na volume by
// restart kontejneru odhlásil všechny uživatele (kap. 15.3).
var dataProtection = builder.Services.AddDataProtection().SetApplicationName("Helpdesk.Web");
var keysPath = builder.Configuration["DataProtection:KeysPath"];
if (!string.IsNullOrWhiteSpace(keysPath))
dataProtection.PersistKeysToFileSystem(new DirectoryInfo(keysPath));
var trustedProxy = builder.Configuration["ReverseProxy:TrustedIp"];
if (!string.IsNullOrWhiteSpace(trustedProxy))
{
if (!IPAddress.TryParse(trustedProxy, out var proxyAddress))
throw new InvalidOperationException("Nastavte platnou IP adresu důvěryhodné reverse proxy.");
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownIPNetworks.Clear();
options.KnownProxies.Clear();
options.KnownProxies.Add(proxyAddress);
});
}
var app = builder.Build();
if (!string.IsNullOrWhiteSpace(trustedProxy))
app.UseForwardedHeaders();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/error");
app.UseHsts();
}
app.UseStatusCodePagesWithReExecute("/error/{0}");
app.UseSecurityHeaders();
var czech = new CultureInfo("cs-CZ");
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(czech),
SupportedCultures = [czech],
SupportedUICultures = [czech],
});
app.UseRouting();
app.UseAuthorization();
app.MapStaticAssets();
app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}").WithStaticAssets();
app.Run();
/// <summary>Vstupní bod – veřejný kvůli testům (WebApplicationFactory).</summary>
public partial class Program;