File size: 2,570 Bytes
0e0fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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();