diff --git a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopOnAbpAuthServerModule.cs b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopOnAbpAuthServerModule.cs index 3cd489bf..7e304d5c 100644 --- a/apps/auth-server/src/EShopOnAbp.AuthServer/EShopOnAbpAuthServerModule.cs +++ b/apps/auth-server/src/EShopOnAbp.AuthServer/EShopOnAbpAuthServerModule.cs @@ -34,206 +34,198 @@ using Volo.Abp.Modularity; using Volo.Abp.UI.Navigation.Urls; using Volo.Abp.VirtualFileSystem; -namespace EShopOnAbp.AuthServer +namespace EShopOnAbp.AuthServer; + +[DependsOn( + typeof(AbpCachingStackExchangeRedisModule), + typeof(AbpAspNetCoreAuthenticationJwtBearerModule), + typeof(AbpEventBusRabbitMqModule), + typeof(AbpBackgroundJobsRabbitMqModule), + typeof(AbpAspNetCoreMvcUiBasicThemeModule), + typeof(AbpAccountWebIdentityServerModule), + typeof(AbpAccountHttpApiModule), + typeof(AbpAccountApplicationModule), + typeof(EShopOnAbpSharedHostingAspNetCoreModule), + typeof(EShopOnAbpSharedLocalizationModule), + typeof(AdministrationServiceEntityFrameworkCoreModule), + typeof(IdentityServiceEntityFrameworkCoreModule) +)] +public class EShopOnAbpAuthServerModule : AbpModule { - [DependsOn( - typeof(AbpCachingStackExchangeRedisModule), - typeof(AbpAspNetCoreAuthenticationJwtBearerModule), - typeof(AbpEventBusRabbitMqModule), - typeof(AbpBackgroundJobsRabbitMqModule), - typeof(AbpAspNetCoreMvcUiBasicThemeModule), - typeof(AbpAccountWebIdentityServerModule), - typeof(AbpAccountHttpApiModule), - typeof(AbpAccountApplicationModule), - typeof(EShopOnAbpSharedHostingAspNetCoreModule), - typeof(EShopOnAbpSharedLocalizationModule), - typeof(AdministrationServiceEntityFrameworkCoreModule), - typeof(IdentityServiceEntityFrameworkCoreModule) - )] - public class EShopOnAbpAuthServerModule : AbpModule + public override void PreConfigureServices(ServiceConfigurationContext context) { - public override void PreConfigureServices(ServiceConfigurationContext context) - { - var hostingEnvironment = context.Services.GetHostingEnvironment(); - - if (!hostingEnvironment.IsDevelopment()) - { - var configuration = context.Services.GetConfiguration(); - - PreConfigure(options => - { - options.AddDeveloperSigningCredential = false; - }); + var hostingEnvironment = context.Services.GetHostingEnvironment(); - PreConfigure(builder => - { - builder.AddSigningCredential(GetSigningCertificate(hostingEnvironment)); - }); - } - } - - public override void ConfigureServices(ServiceConfigurationContext context) + if (!hostingEnvironment.IsDevelopment()) { - Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; - var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); - ConfigureSwagger(context, configuration); - - context.Services.AddAuthentication() - .AddJwtBearer(options => - { - options.Authority = configuration["AuthServer:Authority"]; - options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); - options.Audience = "AccountService"; - }); - - Configure(options => + PreConfigure(options => { - options.IssuerUri = configuration["App:SelfUrl"]; + options.AddDeveloperSigningCredential = false; }); - Configure(options => - { - options.RequestedClaims.AddRange(new[]{ - EShopConstants.AnonymousUserClaimName - }); - }); - - Configure(options => + PreConfigure(builder => { - options.ApplicationName = "AuthServer"; + builder.AddSigningCredential(GetSigningCertificate(hostingEnvironment)); }); + } + } - Configure(options => + public override void ConfigureServices(ServiceConfigurationContext context) + { + Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; + var hostingEnvironment = context.Services.GetHostingEnvironment(); + var configuration = context.Services.GetConfiguration(); + + ConfigureSwagger(context, configuration); + + context.Services.AddAuthentication() + .AddJwtBearer(options => { - options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; - options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"].Split(',')); + options.Authority = configuration["AuthServer:Authority"]; + options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); + options.Audience = "AccountService"; }); - Configure(options => + Configure(options => { options.IssuerUri = configuration["App:SelfUrl"]; }); + + Configure(options => + { + options.RequestedClaims.AddRange(new[] { - options.KeyPrefix = "EShopOnAbp:"; + EShopConstants.AnonymousUserClaimName }); + }); + + Configure(options => { options.ApplicationName = "AuthServer"; }); + + Configure(options => + { + options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; + options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"].Split(',')); + }); - var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); - context.Services - .AddDataProtection() - .PersistKeysToStackExchangeRedis(redis, "EShopOnAbp-Protection-Keys") - .SetApplicationName("eShopOnAbp-AuthServer"); + Configure(options => { options.KeyPrefix = "EShopOnAbp:"; }); - context.Services.AddCors(options => + var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + context.Services + .AddDataProtection() + .PersistKeysToStackExchangeRedis(redis, "EShopOnAbp-Protection-Keys") + .SetApplicationName("eShopOnAbp-AuthServer"); + + context.Services.AddCors(options => + { + options.AddDefaultPolicy(builder => { - options.AddDefaultPolicy(builder => - { - builder - .WithOrigins( - configuration["App:CorsOrigins"] - .Split(",", StringSplitOptions.RemoveEmptyEntries) - .Select(o => o.Trim().RemovePostFix("/")) - .ToArray() - ) - .WithAbpExposedHeaders() - .SetIsOriginAllowedToAllowWildcardSubdomains() - .AllowAnyHeader() - .AllowAnyMethod() - .AllowCredentials(); - }); + builder + .WithOrigins( + configuration["App:CorsOrigins"] + .Split(",", StringSplitOptions.RemoveEmptyEntries) + .Select(o => o.Trim().RemovePostFix("/")) + .ToArray() + ) + .WithAbpExposedHeaders() + .SetIsOriginAllowedToAllowWildcardSubdomains() + .AllowAnyHeader() + .AllowAnyMethod() + .AllowCredentials(); }); + }); #if DEBUG - context.Services.Replace(ServiceDescriptor.Singleton()); + context.Services.Replace(ServiceDescriptor.Singleton()); #endif - if (hostingEnvironment.IsDevelopment()) - { - Configure(options => - { - options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine(hostingEnvironment.ContentRootPath, - $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}shared{Path.DirectorySeparatorChar}EShopOnAbp.Shared.Localization")); - }); - } - } - - public override void OnApplicationInitialization(ApplicationInitializationContext context) + if (hostingEnvironment.IsDevelopment()) { - var app = context.GetApplicationBuilder(); - var env = context.GetEnvironment(); - - var configuration = context.ServiceProvider.GetRequiredService(); - - app.Use(async (ctx, next) => + Configure(options => { - if (ctx.Request.Headers.ContainsKey("from-ingress")) - { - ctx.SetIdentityServerOrigin(configuration["App:SelfUrl"]); - } - - await next(); + options.FileSets.ReplaceEmbeddedByPhysical(Path.Combine( + hostingEnvironment.ContentRootPath, + $"..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}shared{Path.DirectorySeparatorChar}EShopOnAbp.Shared.Localization")); }); + } + } - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + var env = context.GetEnvironment(); - app.UseAbpRequestLocalization(); + var configuration = context.ServiceProvider.GetRequiredService(); - if (!env.IsDevelopment()) + app.Use(async (ctx, next) => + { + if (ctx.Request.Headers.ContainsKey("from-ingress")) { - app.UseErrorPage(); + ctx.SetIdentityServerOrigin(configuration["App:SelfUrl"]); } - app.UseCorrelationId(); - app.UseStaticFiles(); - app.UseRouting(); - app.UseCors(); - app.UseAuthentication(); - app.UseJwtTokenMiddleware(); - app.UseAbpSerilogEnrichers(); - app.UseUnitOfWork(); - app.UseIdentityServer(); - app.UseAuthorization(); - app.UseSwagger(); - app.UseSwaggerUI(options => - { - options.SwaggerEndpoint("/swagger/v1/swagger.json", "Account Service API"); - options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); - options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); - }); - app.UseAuditing(); - app.UseConfiguredEndpoints(); - } - - private X509Certificate2 GetSigningCertificate(IWebHostEnvironment hostingEnv) + await next(); + }); + + if (env.IsDevelopment()) { - const string fileName = "eshoponabp-authserver.pfx"; - const string passPhrase = "780F3C11-0A96-40DE-B335-9848BE88C77D"; - var file = Path.Combine(hostingEnv.ContentRootPath, fileName); + app.UseDeveloperExceptionPage(); + } - if (!File.Exists(file)) - { - throw new FileNotFoundException($"Signing Certificate couldn't found: {file}"); - } + app.UseAbpRequestLocalization(); - return new X509Certificate2(file, passPhrase); + if (!env.IsDevelopment()) + { + app.UseErrorPage(); } - - private void ConfigureSwagger(ServiceConfigurationContext context, IConfiguration configuration) + + app.UseCorrelationId(); + app.UseStaticFiles(); + app.UseRouting(); + app.UseCors(); + app.UseAuthentication(); + app.UseJwtTokenMiddleware(); + app.UseAbpSerilogEnrichers(); + app.UseUnitOfWork(); + app.UseIdentityServer(); + app.UseAuthorization(); + app.UseSwagger(); + app.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "Account Service API"); + options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); + options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); + }); + app.UseAuditing(); + app.UseConfiguredEndpoints(); + } + + private X509Certificate2 GetSigningCertificate(IWebHostEnvironment hostingEnv) + { + const string fileName = "eshoponabp-authserver.pfx"; + const string passPhrase = "780F3C11-0A96-40DE-B335-9848BE88C77D"; + var file = Path.Combine(hostingEnv.ContentRootPath, fileName); + + if (!File.Exists(file)) { - SwaggerWithAuthConfigurationHelper.Configure( - context: context, - authority: configuration["AuthServer:Authority"], - scopes: new Dictionary + throw new FileNotFoundException($"Signing Certificate couldn't found: {file}"); + } + + return new X509Certificate2(file, passPhrase); + } + + private void ConfigureSwagger(ServiceConfigurationContext context, IConfiguration configuration) + { + SwaggerWithAuthConfigurationHelper.Configure( + context: context, + authority: configuration["AuthServer:Authority"], + scopes: new Dictionary + { + /* Requested scopes for authorization code request and descriptions for swagger UI only */ { - /* Requested scopes for authorization code request and descriptions for swagger UI only */ - { - "AccountService", - "Account Service API" - }, + "AccountService", + "Account Service API" }, - apiTitle: "Account Service API" - ); - } + }, + apiTitle: "Account Service API" + ); } } \ No newline at end of file diff --git a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs index 753e8549..a6b63092 100644 --- a/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs +++ b/apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs @@ -40,190 +40,172 @@ using EShopOnAbp.PublicWeb.PaymentMethods; using EShopOnAbp.PaymentService.PaymentMethods; using Microsoft.Extensions.Configuration; -namespace EShopOnAbp.PublicWeb +namespace EShopOnAbp.PublicWeb; + +[DependsOn( + typeof(AbpCachingStackExchangeRedisModule), + typeof(AbpEventBusRabbitMqModule), + typeof(AbpAspNetCoreMvcClientModule), + typeof(AbpAspNetCoreAuthenticationOpenIdConnectModule), + typeof(AbpHttpClientIdentityModelWebModule), + typeof(AbpAspNetCoreMvcUiBasicThemeModule), + typeof(AbpAccountHttpApiClientModule), + typeof(EShopOnAbpSharedHostingAspNetCoreModule), + typeof(EShopOnAbpSharedLocalizationModule), + typeof(CatalogServiceHttpApiClientModule), + typeof(BasketServiceHttpApiClientModule), + typeof(OrderingServiceHttpApiClientModule), + typeof(AbpAspNetCoreSignalRModule), + typeof(PaymentServiceHttpApiClientModule), + typeof(AbpAutoMapperModule) +)] +public class EShopOnAbpPublicWebModule : AbpModule { - [DependsOn( - typeof(AbpCachingStackExchangeRedisModule), - typeof(AbpEventBusRabbitMqModule), - typeof(AbpAspNetCoreMvcClientModule), - typeof(AbpAspNetCoreAuthenticationOpenIdConnectModule), - typeof(AbpHttpClientIdentityModelWebModule), - typeof(AbpAspNetCoreMvcUiBasicThemeModule), - typeof(AbpAccountHttpApiClientModule), - typeof(EShopOnAbpSharedHostingAspNetCoreModule), - typeof(EShopOnAbpSharedLocalizationModule), - typeof(CatalogServiceHttpApiClientModule), - typeof(BasketServiceHttpApiClientModule), - typeof(OrderingServiceHttpApiClientModule), - typeof(AbpAspNetCoreSignalRModule), - typeof(PaymentServiceHttpApiClientModule), - typeof(AbpAutoMapperModule) - )] - public class EShopOnAbpPublicWebModule : AbpModule + public override void PreConfigureServices(ServiceConfigurationContext context) { - public override void PreConfigureServices(ServiceConfigurationContext context) + context.Services.PreConfigure(options => { - context.Services.PreConfigure(options => - { - options.AddAssemblyResource( - typeof(EShopOnAbpResource), - typeof(EShopOnAbpPublicWebModule).Assembly - ); - }); - } + options.AddAssemblyResource( + typeof(EShopOnAbpResource), + typeof(EShopOnAbpPublicWebModule).Assembly + ); + }); + } + + public override void ConfigureServices(ServiceConfigurationContext context) + { + Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; + var hostingEnvironment = context.Services.GetHostingEnvironment(); + var configuration = context.Services.GetConfiguration(); + + context.Services.AddAutoMapperObjectMapper(); + Configure(options => { options.AddMaps(validate: true); }); - public override void ConfigureServices(ServiceConfigurationContext context) + Configure(options => { - Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true; - var hostingEnvironment = context.Services.GetHostingEnvironment(); - var configuration = context.Services.GetConfiguration(); + options.StyleBundles.Configure( + BasicThemeBundles.Styles.Global, + bundle => { bundle.AddContributors(typeof(CartWidgetStyleContributor)); } + ); + }); - context.Services.AddAutoMapperObjectMapper(); - Configure(options => - { - options.AddMaps(validate: true); - }); + Configure(options => { options.IsEnabled = true; }); - Configure(options => - { - options.StyleBundles.Configure( - BasicThemeBundles.Styles.Global, - bundle => - { - bundle.AddContributors(typeof(CartWidgetStyleContributor)); - } - ); - }); + Configure(options => { options.KeyPrefix = "EShopOnAbp:"; }); - Configure(options => - { - options.IsEnabled = true; - }); + Configure(options => { options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; }); - Configure(options => - { - options.KeyPrefix = "EShopOnAbp:"; - }); + ConfigurePayment(configuration); - Configure(options => + context.Services.AddAuthentication(options => + { + options.DefaultScheme = "Cookies"; + options.DefaultChallengeScheme = "oidc"; + }) + .AddCookie("Cookies", options => { options.ExpireTimeSpan = TimeSpan.FromDays(365); }) + .AddAbpOpenIdConnect("oidc", options => { - options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"]; + options.Authority = configuration["AuthServer:Authority"]; + options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); + options.ResponseType = OpenIdConnectResponseType.CodeIdToken; + + options.ClientId = configuration["AuthServer:ClientId"]; + options.ClientSecret = configuration["AuthServer:ClientSecret"]; + + options.SaveTokens = true; + options.GetClaimsFromUserInfoEndpoint = true; + + options.Scope.Add("role"); + options.Scope.Add("email"); + options.Scope.Add("phone"); + options.Scope.Add("AccountService"); + options.Scope.Add("AdministrationService"); + options.Scope.Add("BasketService"); + options.Scope.Add("CatalogService"); + options.Scope.Add("PaymentService"); + options.Scope.Add("OrderingService"); }); - ConfigurePayment(configuration); - - context.Services.AddAuthentication(options => - { - options.DefaultScheme = "Cookies"; - options.DefaultChallengeScheme = "oidc"; - }) - .AddCookie("Cookies", options => - { - options.ExpireTimeSpan = TimeSpan.FromDays(365); - }) - .AddAbpOpenIdConnect("oidc", options => - { - options.Authority = configuration["AuthServer:Authority"]; - options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]); - options.ResponseType = OpenIdConnectResponseType.CodeIdToken; - - options.ClientId = configuration["AuthServer:ClientId"]; - options.ClientSecret = configuration["AuthServer:ClientSecret"]; - - options.SaveTokens = true; - options.GetClaimsFromUserInfoEndpoint = true; - - options.Scope.Add("role"); - options.Scope.Add("email"); - options.Scope.Add("phone"); - options.Scope.Add("AccountService"); - options.Scope.Add("AdministrationService"); - options.Scope.Add("BasketService"); - options.Scope.Add("CatalogService"); - options.Scope.Add("PaymentService"); - options.Scope.Add("OrderingService"); - }); + var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + context.Services + .AddDataProtection() + .PersistKeysToStackExchangeRedis(redis, "EShopOnAbp-Protection-Keys") + .SetApplicationName("eShopOnAbp-PublicWeb"); - var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); - context.Services - .AddDataProtection() - .PersistKeysToStackExchangeRedis(redis, "EShopOnAbp-Protection-Keys") - .SetApplicationName("eShopOnAbp-PublicWeb"); + Configure(options => + { + options.MenuContributors.Add(new EShopOnAbpPublicWebMenuContributor(configuration)); + }); - Configure(options => - { - options.MenuContributors.Add(new EShopOnAbpPublicWebMenuContributor(configuration)); - }); + Configure(options => + { + options.Contributors.Add(new EShopOnAbpPublicWebToolbarContributor()); + }); - Configure(options => + context.Services + .AddReverseProxy() + .LoadFromConfig(configuration.GetSection("ReverseProxy")) + .AddTransforms(builderContext => { - options.Contributors.Add(new EShopOnAbpPublicWebToolbarContributor()); - }); - - context.Services - .AddReverseProxy() - .LoadFromConfig(configuration.GetSection("ReverseProxy")) - .AddTransforms(builderContext => + builderContext.AddRequestTransform(async (transformContext) => { - builderContext.AddRequestTransform(async (transformContext) => - { - transformContext.ProxyRequest.Headers - .Authorization = new AuthenticationHeaderValue( - "Bearer", - await transformContext.HttpContext.GetTokenAsync("access_token") - ); - }); + transformContext.ProxyRequest.Headers + .Authorization = new AuthenticationHeaderValue( + "Bearer", + await transformContext.HttpContext.GetTokenAsync("access_token") + ); }); - } + }); + } - private void ConfigurePayment(IConfiguration configuration) + private void ConfigurePayment(IConfiguration configuration) + { + Configure(options => { - Configure(options => - { - options.PaymentSuccessfulCallbackUrl = configuration["App:SelfUrl"].EnsureEndsWith('/') + "PaymentCompleted"; - }); + options.PaymentSuccessfulCallbackUrl = + configuration["App:SelfUrl"].EnsureEndsWith('/') + "PaymentCompleted"; + }); - Configure(options => - { - options.ConfigureIcon(PaymentMethodNames.PayPal, "fa-cc-paypal paypal"); - }); - } + Configure(options => + { + options.ConfigureIcon(PaymentMethodNames.PayPal, "fa-cc-paypal paypal"); + }); + } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + var env = context.GetEnvironment(); + + app.Use((ctx, next) => { - var app = context.GetApplicationBuilder(); - var env = context.GetEnvironment(); - - app.Use((ctx, next) => - { - ctx.Request.Scheme = "https"; - return next(); - }); + ctx.Request.Scheme = "https"; + return next(); + }); - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } - app.UseAbpRequestLocalization(); + app.UseAbpRequestLocalization(); - if (!env.IsDevelopment()) - { - app.UseErrorPage(); - } - - app.UseCorrelationId(); - app.UseStaticFiles(); - app.UseRouting(); - // app.UseHttpMetrics(); - app.UseAuthentication(); - app.UseAbpSerilogEnrichers(); - app.UseAuthorization(); - app.UseConfiguredEndpoints(endpoints => - { - endpoints.MapReverseProxy(); - // endpoints.MapMetrics(); - }); + if (!env.IsDevelopment()) + { + app.UseErrorPage(); } + + app.UseCorrelationId(); + app.UseStaticFiles(); + app.UseRouting(); + // app.UseHttpMetrics(); + app.UseAuthentication(); + app.UseAbpSerilogEnrichers(); + app.UseAuthorization(); + app.UseConfiguredEndpoints(endpoints => + { + endpoints.MapReverseProxy(); + // endpoints.MapMetrics(); + }); } -} +} \ No newline at end of file diff --git a/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs b/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs index 33d9554e..e9af7505 100644 --- a/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs +++ b/gateways/web-public/src/EShopOnAbp.WebPublicGateway/EShopOnAbpWebPublicGatewayModule.cs @@ -1,5 +1,4 @@ -using System; -using EShopOnAbp.Shared.Hosting.Gateways; +using EShopOnAbp.Shared.Hosting.Gateways; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -8,84 +7,82 @@ using Ocelot.Middleware; using System.Collections.Generic; using System.Linq; using EShopOnAbp.Shared.Hosting.AspNetCore; -using Microsoft.AspNetCore.Cors; using Volo.Abp; using Volo.Abp.Modularity; -namespace EShopOnAbp.WebPublicGateway +namespace EShopOnAbp.WebPublicGateway; + +[DependsOn( + typeof(EShopOnAbpSharedHostingGatewaysModule) +)] +public class EShopOnAbpWebPublicGatewayModule : AbpModule { - [DependsOn( - typeof(EShopOnAbpSharedHostingGatewaysModule) - )] - public class EShopOnAbpWebPublicGatewayModule : AbpModule + public override void ConfigureServices(ServiceConfigurationContext context) { - public override void ConfigureServices(ServiceConfigurationContext context) - { - var configuration = context.Services.GetConfiguration(); - var hostingEnvironment = context.Services.GetHostingEnvironment(); + var configuration = context.Services.GetConfiguration(); + var hostingEnvironment = context.Services.GetHostingEnvironment(); - SwaggerWithAuthConfigurationHelper.Configure( - context: context, - authority: configuration["AuthServer:Authority"], - scopes: new Dictionary /* Requested scopes for authorization code request and descriptions for swagger UI only */ - { - { "AccountService", "Account Service API" }, - { "IdentityService", "Identity Service API" }, - { "AdministrationService", "Administration Service API" }, - { "CatalogService", "Catalog Service API" }, - { "BasketService", "Basket Service API" }, - { "PaymentService", "Payment Service API" }, - { "OrderingService", "Ordering Service API" }, - }, - apiTitle: "WebPublic Gateway" - ); - } + SwaggerWithAuthConfigurationHelper.Configure( + context: context, + authority: configuration["AuthServer:Authority"], + scopes: new Dictionary /* Requested scopes for authorization code request and descriptions for swagger UI only */ + { + { "AccountService", "Account Service API" }, + { "IdentityService", "Identity Service API" }, + { "AdministrationService", "Administration Service API" }, + { "CatalogService", "Catalog Service API" }, + { "BasketService", "Basket Service API" }, + { "PaymentService", "Payment Service API" }, + { "OrderingService", "Ordering Service API" }, + }, + apiTitle: "WebPublic Gateway" + ); + } - public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - var app = context.GetApplicationBuilder(); - var env = context.GetEnvironment(); + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + var env = context.GetEnvironment(); - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } - app.UseCorrelationId(); - app.UseAbpSerilogEnrichers(); - app.UseSwagger(); - app.UseSwaggerUI(options => - { - var configuration = context.ServiceProvider.GetRequiredService(); - var routes = configuration.GetSection("Routes").Get>(); - var routedServices = routes - .GroupBy(t => t.ServiceKey) - .Select(r => r.First()) - .Distinct(); + app.UseCorrelationId(); + app.UseAbpSerilogEnrichers(); + app.UseSwagger(); + app.UseSwaggerUI(options => + { + var configuration = context.ServiceProvider.GetRequiredService(); + var routes = configuration.GetSection("Routes").Get>(); + var routedServices = routes + .GroupBy(t => t.ServiceKey) + .Select(r => r.First()) + .Distinct(); - foreach (var config in routedServices) - { - var url = - $"{config.DownstreamScheme}://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}:{config.DownstreamHostAndPorts.FirstOrDefault()?.Port}"; - if (!env.IsDevelopment()) - { - url = $"https://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}"; - } - - options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); - options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); - options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); - } - }); - app.MapWhen( - ctx => ctx.Request.Path.ToString().TrimEnd('/').Equals(""), - app2 => + foreach (var config in routedServices) + { + var url = + $"{config.DownstreamScheme}://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}:{config.DownstreamHostAndPorts.FirstOrDefault()?.Port}"; + if (!env.IsDevelopment()) { - app2.UseRouting(); - app2.UseConfiguredEndpoints(); + url = $"https://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}"; } - ); - app.UseOcelot().Wait(); - } + + options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); + options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); + options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); + } + }); + app.MapWhen( + ctx => ctx.Request.Path.ToString().TrimEnd('/').Equals(""), + app2 => + { + app2.UseRouting(); + app2.UseConfiguredEndpoints(); + } + ); + app.UseOcelot().Wait(); } } \ No newline at end of file diff --git a/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs b/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs index 34bfa808..87974f87 100644 --- a/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs +++ b/gateways/web/src/EShopOnAbp.WebGateway/EShopOnAbpWebGatewayModule.cs @@ -9,104 +9,103 @@ using System; using System.Collections.Generic; using System.Linq; using EShopOnAbp.Shared.Hosting.AspNetCore; -using Microsoft.OpenApi.Models; using Volo.Abp; using Volo.Abp.Modularity; -namespace EShopOnAbp.WebGateway -{ - [DependsOn( +namespace EShopOnAbp.WebGateway; + +[DependsOn( typeof(EShopOnAbpSharedHostingGatewaysModule) )] - public class EShopOnAbpWebGatewayModule : AbpModule +public class EShopOnAbpWebGatewayModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) { - public override void ConfigureServices(ServiceConfigurationContext context) - { - var configuration = context.Services.GetConfiguration(); - var hostingEnvironment = context.Services.GetHostingEnvironment(); - - SwaggerWithAuthConfigurationHelper.Configure( - context: context, - authority: configuration["AuthServer:Authority"], - scopes: new Dictionary /* Requested scopes for authorization code request and descriptions for swagger UI only */ + var configuration = context.Services.GetConfiguration(); + var hostingEnvironment = context.Services.GetHostingEnvironment(); + + SwaggerWithAuthConfigurationHelper.Configure( + context: context, + authority: configuration["AuthServer:Authority"], + scopes: new + Dictionary /* Requested scopes for authorization code request and descriptions for swagger UI only */ { - { "AccountService", "Account Service API" }, - { "IdentityService", "Identity Service API" }, - { "AdministrationService", "Administration Service API" }, - { "CatalogService", "Catalog Service API" }, - { "BasketService", "Basket Service API" }, - { "PaymentService", "Payment Service API" }, - { "OrderingService", "Ordering Service API" }, + {"AccountService", "Account Service API"}, + {"IdentityService", "Identity Service API"}, + {"AdministrationService", "Administration Service API"}, + {"CatalogService", "Catalog Service API"}, + {"BasketService", "Basket Service API"}, + {"PaymentService", "Payment Service API"}, + {"OrderingService", "Ordering Service API"}, }, - apiTitle: "Web Gateway" - ); - - context.Services.AddCors(options => + apiTitle: "Web Gateway" + ); + + context.Services.AddCors(options => + { + options.AddDefaultPolicy(builder => { - options.AddDefaultPolicy(builder => - { - builder - .WithOrigins( - configuration["App:CorsOrigins"] - .Split(",", StringSplitOptions.RemoveEmptyEntries) - .Select(o => o.Trim().RemovePostFix("/")) - .ToArray() - ) - .WithAbpExposedHeaders() - .SetIsOriginAllowedToAllowWildcardSubdomains() - .AllowAnyHeader() - .AllowAnyMethod() - .AllowCredentials(); - }); + builder + .WithOrigins( + configuration["App:CorsOrigins"] + .Split(",", StringSplitOptions.RemoveEmptyEntries) + .Select(o => o.Trim().RemovePostFix("/")) + .ToArray() + ) + .WithAbpExposedHeaders() + .SetIsOriginAllowedToAllowWildcardSubdomains() + .AllowAnyHeader() + .AllowAnyMethod() + .AllowCredentials(); }); - } + }); + } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + var env = context.GetEnvironment(); + + if (env.IsDevelopment()) { - var app = context.GetApplicationBuilder(); - var env = context.GetEnvironment(); + app.UseDeveloperExceptionPage(); + } - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } + app.UseCorrelationId(); + app.UseAbpSerilogEnrichers(); + app.UseCors(); + app.UseSwagger(); + app.UseSwaggerUI(options => + { + var configuration = context.ServiceProvider.GetRequiredService(); + var routes = configuration.GetSection("Routes").Get>(); + var routedServices = routes + .GroupBy(t => t.ServiceKey) + .Select(r => r.First()) + .Distinct(); - app.UseCorrelationId(); - app.UseAbpSerilogEnrichers(); - app.UseCors(); - app.UseSwagger(); - app.UseSwaggerUI(options => + foreach (var config in routedServices) { - var configuration = context.ServiceProvider.GetRequiredService(); - var routes = configuration.GetSection("Routes").Get>(); - var routedServices = routes - .GroupBy(t => t.ServiceKey) - .Select(r => r.First()) - .Distinct(); - - foreach (var config in routedServices) - { - var url = - $"{config.DownstreamScheme}://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}:{config.DownstreamHostAndPorts.FirstOrDefault()?.Port}"; - if (!env.IsDevelopment()) - { - url = $"https://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}"; - } - - options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); - options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); - options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); - } - }); - app.MapWhen( - ctx => ctx.Request.Path.ToString().TrimEnd('/').Equals(""), - app2 => + var url = + $"{config.DownstreamScheme}://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}:{config.DownstreamHostAndPorts.FirstOrDefault()?.Port}"; + if (!env.IsDevelopment()) { - app2.UseRouting(); - app2.UseConfiguredEndpoints(); + url = $"https://{config.DownstreamHostAndPorts.FirstOrDefault()?.Host}"; } - ); - app.UseOcelot().Wait(); - } + + options.SwaggerEndpoint($"{url}/swagger/v1/swagger.json", $"{config.ServiceKey} API"); + options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]); + options.OAuthClientSecret(configuration["AuthServer:SwaggerClientSecret"]); + } + }); + app.MapWhen( + ctx => ctx.Request.Path.ToString().TrimEnd('/').Equals(""), + app2 => + { + app2.UseRouting(); + app2.UseConfiguredEndpoints(); + } + ); + app.UseOcelot().Wait(); } -} +} \ No newline at end of file