-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
177 lines (142 loc) · 5.91 KB
/
Program.cs
File metadata and controls
177 lines (142 loc) · 5.91 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Reflection;
using Microsoft.AspNetCore.Hosting.StaticWebAssets;
using Microsoft.AspNetCore.HttpOverrides;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.DataProtection;
using StackExchange.Redis;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using MudBlazor.Extensions;
using Web.Components;
using Web.Services.OidcCookie;
// Version and copyright message
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Lighthouse Notes Web");
Console.WriteLine(Assembly.GetEntryAssembly()!.GetName().Version?.ToString(3));
Console.WriteLine();
Console.WriteLine("(C) Copyright 2024 Lighthouse Notes");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
// Create builder
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Static assets
StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
// Add MVC controllers
builder.Services.AddControllers();
builder.Services.AddRazorPages();
// Add standard razor services
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Change signalRs maximum message size so we can upload large images
builder.Services.AddSignalR(o =>
{
o.MaximumReceiveMessageSize = 100000000; // bytes - 100mb
});
// Use Redis for key storage if running in production
if (builder.Environment.IsProduction())
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(builder.Configuration.GetConnectionString("Redis") ??
throw new InvalidOperationException(
"Connection string 'Redis' not found in appsettings.json or environment variable!"));
builder.Services.AddDataProtection()
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");
}
// Add certificate forwarding for Nginx reverse proxy
builder.Services.AddCertificateForwarding(options =>
{
options.CertificateHeader = "X-SSL-CERT";
options.HeaderConverter = headerValue =>
{
X509Certificate2 clientCertificate = new(System.Web.HttpUtility.UrlDecodeToBytes(headerValue));
return clientCertificate;
};
});
// Add forward headers for reverse proxy
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
// Add Oidc Authentication
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.Authority = builder.Configuration["Authentication:Authority"];
options.ClientId = builder.Configuration["Authentication:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:ClientSecret"];
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.ResponseType = OpenIdConnectResponseType.Code;
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.MapInboundClaims = true;
options.Scope.Add(OpenIdConnectScope.OfflineAccess);
options.Scope.Add(OpenIdConnectScope.OpenId);
options.Scope.Add(OpenIdConnectScope.Profile);
options.Scope.Add(OpenIdConnectScope.Email);
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
// Configure cookie refresh
builder.Services.ConfigureCookieOidcRefresh(CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme);
// Add cascading authentication
builder.Services.AddCascadingAuthenticationState();
// Localization
builder.Services.AddLocalization();
// Add MudBlazor Services
builder.Services.AddMudServicesWithExtensions(option => { option.PopoverOptions.ThrowOnDuplicateProvider = false; });
// Other services
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();
// Lighthouse Notes API services
builder.Services.AddScoped<LighthouseNotesAPIGet>();
builder.Services.AddScoped<LighthouseNotesAPIPost>();
builder.Services.AddScoped<LighthouseNotesAPIPut>();
builder.Services.AddScoped<LighthouseNotesAPIDelete>();
// Services
builder.Services.AddScoped<SpinnerService>();
builder.Services.AddScoped<TokenService>();
builder.Services.AddScoped<ISettingsService, SettingsService>();
// Build app
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (app.Environment.IsProduction())
{
// Use certificate forwarding and header forwarding as production environment runs behind a reverse proxy
app.UseCertificateForwarding();
app.UseForwardedHeaders();
// Use exception handler
app.UseExceptionHandler("/error");
}
// HTTPS Redirection
app.UseHttpsRedirection();
app.UseAntiforgery();
// Static files
app.UseStaticFiles();
// Use Authentication and Authorization
app.UseAuthentication();
app.UseAuthorization();
// Create an array of supported cultures
string[] supportedCultures = ["en-US", "en-GB"];
// Create localization options
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
// Use request localization and the options defined above
app.UseRequestLocalization(localizationOptions);
// Use Mud extensions middleware
app.Use(MudExWebApp.MudExMiddleware);
// Map MVC controllers
app.MapControllers();
// Map blazor pages and set fallback page
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.MapRazorPages();
// Redirect to /error with the status code in the url if there is an error status code
app.UseStatusCodePagesWithRedirects("/error/{0}");
// Run app
app.Run();