|
16 | 16 | using CodeBeam.UltimateAuth.IdentityLab.Services; |
17 | 17 | using CodeBeam.UltimateAuth.Sample.Shared.Infrastructure; |
18 | 18 | using CodeBeam.UltimateAuth.Server.Extensions; |
| 19 | +using Microsoft.AspNetCore.DataProtection; |
| 20 | +using Microsoft.AspNetCore.HttpOverrides; |
19 | 21 | using Microsoft.EntityFrameworkCore; |
20 | 22 | using MudBlazor.Services; |
21 | 23 | using MudExtensions.Services; |
|
108 | 110 | builder.Services.AddScoped<ISeedContributor, LabDemoRoleSeedContributor>(); |
109 | 111 | builder.Services.AddLogging(); |
110 | 112 |
|
| 113 | +builder.Services.Configure<ForwardedHeadersOptions>(options => |
| 114 | +{ |
| 115 | + options.ForwardedHeaders = |
| 116 | + ForwardedHeaders.XForwardedFor | |
| 117 | + ForwardedHeaders.XForwardedProto; |
| 118 | + |
| 119 | + if (builder.Environment.IsProduction()) |
| 120 | + { |
| 121 | + var trustedProxy = builder.Configuration["ReverseProxy:KnownProxy"] |
| 122 | + ?? throw new InvalidOperationException("ReverseProxy:KnownProxy must be configured in production."); |
| 123 | + |
| 124 | + if (!System.Net.IPAddress.TryParse(trustedProxy, out var proxyAddress)) |
| 125 | + { |
| 126 | + throw new InvalidOperationException("ReverseProxy:KnownProxy must be a valid IP address."); |
| 127 | + } |
| 128 | + |
| 129 | + options.KnownProxies.Add(proxyAddress); |
| 130 | + } |
| 131 | +}); |
| 132 | + |
| 133 | +if (builder.Environment.IsProduction()) |
| 134 | +{ |
| 135 | + var keysPath = builder.Configuration["DataProtection:KeysPath"] |
| 136 | + ?? throw new InvalidOperationException("DataProtection:KeysPath must be configured in production."); |
| 137 | + |
| 138 | + builder.Services.AddDataProtection() |
| 139 | + .SetApplicationName("UltimateAuth.IdentityLab") |
| 140 | + .PersistKeysToFileSystem(new DirectoryInfo(keysPath)); |
| 141 | +} |
| 142 | + |
111 | 143 | var app = builder.Build(); |
112 | 144 |
|
113 | 145 | activityEventBridge.Initialize(app.Services); |
|
120 | 152 | app.UseHsts(); |
121 | 153 | } |
122 | 154 | app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true); |
| 155 | +app.UseForwardedHeaders(); |
123 | 156 | app.UseHttpsRedirection(); |
124 | 157 |
|
125 | 158 | app.UseUltimateAuthWithAspNetCore(); |
|
133 | 166 | .AddInteractiveServerRenderMode() |
134 | 167 | .AddUltimateAuthRoutes(UAuthAssemblies.BlazorClient()); |
135 | 168 |
|
| 169 | +if (args.Contains("--migrate", StringComparer.OrdinalIgnoreCase)) |
| 170 | +{ |
| 171 | + await LabDatabaseMigrator.MigrateAsync(app.Services); |
| 172 | + return; |
| 173 | +} |
| 174 | + |
136 | 175 | await app.SeedIdentityLabAsync(); |
137 | 176 |
|
138 | 177 | app.Run(); |
0 commit comments