-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (86 loc) · 3.07 KB
/
Copy pathProgram.cs
File metadata and controls
108 lines (86 loc) · 3.07 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http.Features;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.MaxRequestBodySize = 10485760; // 10MB limit
});
builder.WebHost.UseUrls("http://0.0.0.0:80");
// Add services to the container.
builder.Services.AddRazorPages();
// Add authentication **before authorization**
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Login"; // Redirect users to login page
options.LogoutPath = "/Logout";
options.AccessDeniedPath = "/AccessDenied";
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.SlidingExpiration = true;
});
builder.Services.AddAuthorization();
// Add database context for PostgreSQL
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add session support
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(15);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// Enable HttpContext Access
builder.Services.AddHttpContextAccessor();
// IT Admin Dash
builder.Services.AddCoreAdmin();
// Ensure database migrations run at startup
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
// Configure StaticFileOptions for MIME types
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".ttf"] = "font/ttf";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider,
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
ctx.Context.Response.Headers["Pragma"] = "no-cache";
ctx.Context.Response.Headers["Expires"] = "0";
}
});
app.MapDefaultControllerRoute();
// Enable custom LessMiddleware
app.UseMiddleware<LessMiddleware>();
app.UseMiddleware<FileUploadMiddleware>();
app.UseMiddleware<LogoutMiddleware>();
// **ORDER MATTERS: Ensure authentication runs before routing**
app.UseSession();
app.UseRouting();
app.UseAuthentication(); // Ensures authentication is properly registered
app.UseAuthorization();
app.MapRazorPages();
// Add alias route for CoreAdmin
app.UseCoreAdminCustomUrl("IT");
app.UseCoreAdminCustomTitle("Doug's Trucks");
app.Run();