using System.Collections.Generic; using System.Linq; using System.Security.Claims; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Ocelot.DependencyInjection; using Ocelot.Middleware; using Volo.Abp; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; using Volo.Abp.EntityFrameworkCore.SqlServer; using Volo.Abp.Identity; using Volo.Abp.Modularity; using Volo.Abp.Security.Claims; using Win_in.Sfs.Shared.Host; namespace InternalGateway; [DependsOn( typeof(AbpAutofacModule), typeof(AbpIdentityHttpApiModule), typeof(AbpEntityFrameworkCoreSqlServerModule), typeof(AbpAspNetCoreSerilogModule) )] public class InternalGatewayHostModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); _ = context.Services.GetHostingEnvironment(); context.SetConsoleTitleOfWebApp("Internal.Gateway"); ConfigureAuthentication(context, configuration); //ConfigureSql(); //ConfigureRedis(context, configuration, hostingEnvironment); // ConfigureSwaggerServices(context); context.Services.AddOcelot(context.Services.GetConfiguration()); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); app.UseCorrelationId(); app.UseRouting(); app.UseAuthentication(); //app.UseMultiTenancy(); //app.UseAuthorization(); app.Use(async (ctx, next) => { var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); var map = new Dictionary() { { "sub", AbpClaimTypes.UserId }, { "role", AbpClaimTypes.Role }, { "email", AbpClaimTypes.Email }, { "name", AbpClaimTypes.UserName }, { "tenantid", AbpClaimTypes.TenantId } }; var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.ContainsKey(p.Type)).ToList(); currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); await next().ConfigureAwait(false); }); // app.UseSwagger(); // app.UseSwaggerUI(options => // { // options.SwaggerEndpoint("/swagger/v1/swagger.json", "Business Service API"); // }); // // app.MapWhen( // ctx => ctx.Request.Path.ToString().StartsWith("/api/abp/") || // ctx.Request.Path.ToString().StartsWith("/Abp/"), // app2 => // { // app2.UseRouting(); // app2.UseConfiguredEndpoints(); // } // ); app.UseOcelot().Wait(); app.UseAbpSerilogEnrichers(); } private static void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration) { context.Services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.Authority = configuration["AuthServer:Authority"]; options.RequireHttpsMetadata = false; options.ApiName = "InternalGateway"; }); } }