-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (106 loc) · 3.92 KB
/
Program.cs
File metadata and controls
126 lines (106 loc) · 3.92 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
123
124
125
126
using InertiaCore.Extensions;
using Microsoft.AspNetCore.Identity;
using PingCRM.Data;
using PingCRM.Extensions;
using PingCRM.Middleware;
using PingCRM.Models;
using Microsoft.AspNetCore.HttpOverrides;
using System.Net;
var builder = WebApplication.CreateBuilder(args);
// Configure logging to suppress HTTPS development certificate warnings
builder.Logging.AddFilter("Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer", LogLevel.Error);
// Add database services
builder.Services.AddDatabaseServices(builder.Configuration);
// Add Identity services
builder.Services.AddIdentity<User, IdentityRole<int>>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 1;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Configure authentication
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/login";
options.LogoutPath = "/logout";
options.AccessDeniedPath = "/login";
options.Cookie.Name = "PingCRM.Auth";
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromDays(30);
});
// https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-9.0
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
var forwardedHeadersConfig = builder.Configuration.GetSection("ForwardedHeaders");
// Set forwarded headers
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// Configure forward limit if specified
var forwardLimit = forwardedHeadersConfig.GetValue<int?>("ForwardLimit");
if (forwardLimit.HasValue)
{
options.ForwardLimit = forwardLimit.Value;
}
// Configure custom header name if specified
var customHeaderName = forwardedHeadersConfig.GetValue<string>("ForwardedForHeaderName");
if (!string.IsNullOrEmpty(customHeaderName))
{
options.ForwardedForHeaderName = customHeaderName;
}
// Add known proxies from configuration
var knownProxies = forwardedHeadersConfig.GetSection("KnownProxies").Get<string[]>();
if (knownProxies != null)
{
foreach (var proxy in knownProxies)
{
if (IPAddress.TryParse(proxy.Trim(), out var ipAddress))
{
options.KnownProxies.Add(ipAddress);
}
}
}
});
// Add services to the container.
builder.Services.AddControllersWithViews()
.AddSessionStateTempDataProvider();
builder.Services.AddSession(options =>
{
options.Cookie.Name = "PingCRM.Session";
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
builder.Services.AddInertia();
builder.Services.AddViteHelper();
// Configure CSRF/Antiforgery protection
builder.Services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
app.UseForwardedHeaders();
}
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.UseInertia();
app.UseMiddleware<CsrfMiddleware>();
app.UseMiddleware<ExceptionMiddleware>();
app.UseMiddleware<HandleInertiaRequests>();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
// Initialize database
await app.InitializeDatabaseAsync();
app.Run();