freespace-freemind / Program.cs
SmartySmile's picture
Upload 45 files
0e0fe0a verified
Raw History Blame Contribute Delete
2.57 kB
using PortWatcher.Services;
var builder = WebApplication.CreateBuilder(args);
// Configure Kestrel with explicit HTTPS settings
builder.WebHost.ConfigureKestrel(serverOptions =>
{
var httpSettings = builder.Configuration.GetSection("HttpSettings");
int httpPort = int.TryParse(httpSettings["HttpPort"], out var hp) ? hp : 5293;
int httpsPort = int.TryParse(httpSettings["HttpsPort"], out var hsp) ? hsp : 7145;
serverOptions.Listen(System.Net.IPAddress.Any, httpPort);
serverOptions.Listen(System.Net.IPAddress.Any, httpsPort,
listenOptions => listenOptions.UseHttps());
});
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
builder.Services.AddScoped<IBenutzerverwaltungService, BenutzerverwaltungService>();
builder.Services.AddScoped<IPortMonitoringService, PortMonitoringService>();
builder.Services.AddScoped<IPortAnalysisService, PortAnalysisService>();
builder.Services.AddScoped<IHttpsCertificateService, HttpsCertificateService>();
var app = builder.Build();
// Initialize default admin user on startup
using (var scope = app.Services.CreateScope())
{
var benutzerverwaltung = scope.ServiceProvider.GetRequiredService<IBenutzerverwaltungService>();
benutzerverwaltung.InitializeDefaultAdminAsync().Wait();
// Initialize HTTPS certificate management
var certService = scope.ServiceProvider.GetRequiredService<IHttpsCertificateService>();
certService.InitializeCertificateAsync().Wait();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// HSTS: 1 year for production
app.UseHsts();
}
else
{
// Development: HSTS disabled for development
// HSTS is only for production
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
// Middleware zur Authentifizierungsprüfung
app.Use(async (context, next) =>
{
var path = context.Request.Path.Value?.ToLower() ?? string.Empty;
var benutzerId = context.Session.GetInt32("BenutzerId");
if (!path.Contains("/login") && !path.Contains("/anleitung") && benutzerId == null)
{
context.Response.Redirect("/Auth/Login");
return;
}
await next();
});
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();