-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (105 loc) · 3.47 KB
/
Program.cs
File metadata and controls
122 lines (105 loc) · 3.47 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using AuthManagerEnterprise.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication;
using System.Security.Claims;
var builder = WebApplication.CreateBuilder(args);
// ==========================================
// 1. DYNAMIC CONFIGURATION (Local vs Render)
// ==========================================
// Handle Port: Render uses a $PORT variable. Local uses launchSettings.json.
if (!builder.Environment.IsDevelopment())
{
var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
builder.WebHost.UseUrls($"http://*:{port}");
}
// Handle Connection String:
// 1. Checks appsettings.Development.json (Local)
// 2. Checks Environment Variables (Render)
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
// =======================
// 2. MVC & AUTH SERVICES
// =======================
builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication("Cookies")
.AddCookie("Cookies", options =>
{
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/Login";
options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.SlidingExpiration = true;
});
var app = builder.Build();
// ==========================================
// 3. AUTO-MIGRATE: Run on Startup
// ==========================================
using (var scope = app.Services.CreateScope())
{
try
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Only migrate if we have a connection string to avoid startup crashes
if (!string.IsNullOrEmpty(connectionString))
{
db.Database.Migrate();
}
}
catch (Exception ex)
{
// This will show in your VS Debug Console or Render Logs
Console.WriteLine($"Migration Error: {ex.Message}");
}
}
// =======================
// 4. MIDDLEWARE PIPELINE
// =======================
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// Keep Redirection off locally if you have SSL cert issues,
// but it's generally safe for Render.
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
// =======================
// 5. GLOBAL BLOCK CHECK
// =======================
app.Use(async (context, next) =>
{
var path = context.Request.Path.Value?.ToLower() ?? "";
// Allow login/register without being blocked
if (path.Contains("/account/login") || path.Contains("/account/register") || path.StartsWith("/lib") || path.StartsWith("/css"))
{
await next();
return;
}
if (context.User.Identity?.IsAuthenticated == true)
{
var db = context.RequestServices.GetRequiredService<ApplicationDbContext>();
var userId = context.User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId != null)
{
var user = await db.Users.FindAsync(int.Parse(userId));
if (user == null || user.Status == "blocked")
{
await context.SignOutAsync("Cookies");
context.Response.Redirect("/Account/Login");
return;
}
}
}
await next();
});
app.MapControllerRoute(
name: "default",
pattern: "{controller=Account}/{action=Login}/{id?}");
app.Run();