|
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.AspNetCore.Builder; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.AspNetCore.HttpOverrides; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using OrchardCore.Modules; |
| 8 | +using OrchardCoreContrib.Modules.Web; |
| 9 | +using OrchardCoreContrib.Testing; |
| 10 | +using OrchardCoreContrib.Testing.Security; |
| 11 | + |
| 12 | +namespace OrchardCoreContrib.HealthChecks.Tests.Tests; |
| 13 | + |
| 14 | +public class OrchardCoreStartup(IConfiguration configuration) |
| 15 | +{ |
| 16 | + public void ConfigureServices(IServiceCollection services) |
| 17 | + { |
| 18 | + services.AddOrchardCms(builder => builder |
| 19 | + .AddSetupFeatures("OrchardCore.Tenants") |
| 20 | + .AddTenantFeatures("OrchardCoreContrib.HealthChecks.IPRestriction") |
| 21 | + .ConfigureServices(serviceCollection => |
| 22 | + { |
| 23 | + serviceCollection.AddScoped<IAuthorizationHandler, PermissionContextAuthorizationHandler>(sp => |
| 24 | + new PermissionContextAuthorizationHandler(sp.GetRequiredService<IHttpContextAccessor>(), SiteContextOptions.PermissionsContexts)); |
| 25 | + |
| 26 | + serviceCollection.AddSingleton<IConfiguration>(AddHealthChecksConfiguration()); |
| 27 | + }) |
| 28 | + .Configure(appBuilder => appBuilder.UseAuthorization())); |
| 29 | + |
| 30 | + services.AddSingleton<IModuleNamesProvider>(new ModuleNamesProvider(typeof(Program).Assembly)); |
| 31 | + } |
| 32 | + |
| 33 | + public void Configure(IApplicationBuilder app) |
| 34 | + { |
| 35 | + var forwardedHeadersOptions = new ForwardedHeadersOptions |
| 36 | + { |
| 37 | + ForwardedHeaders = ForwardedHeaders.XForwardedFor |
| 38 | + }; |
| 39 | + |
| 40 | + forwardedHeadersOptions.KnownNetworks.Clear(); |
| 41 | + forwardedHeadersOptions.KnownProxies.Clear(); |
| 42 | + |
| 43 | + app.UseForwardedHeaders(forwardedHeadersOptions); |
| 44 | + app.UseOrchardCore(); |
| 45 | + } |
| 46 | + |
| 47 | + private IConfigurationRoot AddHealthChecksConfiguration() |
| 48 | + { |
| 49 | + var newConfiguration = new Dictionary<string, string> |
| 50 | + { |
| 51 | + { $"{Constants.ConfigurationKey}:{nameof(HealthChecksOptions.Url)}", "/health" }, |
| 52 | + { $"{Constants.ConfigurationKey}:AllowedIPs:0", "127.0.0.1" }, |
| 53 | + { $"{Constants.ConfigurationKey}:AllowedIPs:1", "::1" } |
| 54 | + }; |
| 55 | + |
| 56 | + return new ConfigurationBuilder() |
| 57 | + .AddConfiguration(configuration) |
| 58 | + .AddInMemoryCollection(newConfiguration) |
| 59 | + .Build(); |
| 60 | + } |
| 61 | +} |
0 commit comments