From af4b9866a8b4d25036aac2d73f6d3bb64ef9db2e Mon Sep 17 00:00:00 2001 From: Pc Date: Fri, 24 Feb 2023 13:56:10 +0330 Subject: [PATCH] fix: Fix 401 in gateway --- booking.rest | 3 ++- src/ApiGateway/src/Program.cs | 14 +------------- src/ApiGateway/src/appsettings.docker.json | 7 ------- src/ApiGateway/src/appsettings.json | 9 +-------- .../Infrastructure/InfrastructureExtensions.cs | 10 ++++++++++ 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/booking.rest b/booking.rest index b3e4d4e..049a1f1 100644 --- a/booking.rest +++ b/booking.rest @@ -1,9 +1,10 @@ # https://github.com/Huachao/vscode-restclient @api-gateway=https://localhost:5000 -@identity-api=https://localhost:5005 +@identity-api=http://localhost:6005 @flight-api=https://localhost:5003 @passenger-api=https://localhost:5012 @booking-api=https://localhost:5010 + @contentType = application/json @flightid = 1 @passengerId = 1 diff --git a/src/ApiGateway/src/Program.cs b/src/ApiGateway/src/Program.cs index 0e586e2..96cba15 100644 --- a/src/ApiGateway/src/Program.cs +++ b/src/ApiGateway/src/Program.cs @@ -1,8 +1,6 @@ -using BuildingBlocks.Jwt; using BuildingBlocks.Logging; using BuildingBlocks.Web; using Figgle; -using Microsoft.AspNetCore.Authentication; using Serilog; var builder = WebApplication.CreateBuilder(args); @@ -13,7 +11,6 @@ Console.WriteLine(FiggleFonts.Standard.Render(appOptions.Name)); builder.AddCustomSerilog(env); -builder.Services.AddJwt(); builder.Services.AddControllers(); builder.Services.AddHttpContextAccessor(); @@ -31,16 +28,7 @@ app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); - endpoints.MapReverseProxy(proxyPipeline => - { - proxyPipeline.Use(async (context, next) => - { - var token = await context.GetTokenAsync("access_token"); - context.Request.Headers["Authorization"] = $"Bearer {token}"; - - await next().ConfigureAwait(false); - }); - }); + endpoints.MapReverseProxy(); }); app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name)); diff --git a/src/ApiGateway/src/appsettings.docker.json b/src/ApiGateway/src/appsettings.docker.json index c603cea..5d836de 100644 --- a/src/ApiGateway/src/appsettings.docker.json +++ b/src/ApiGateway/src/appsettings.docker.json @@ -35,12 +35,5 @@ } } } - }, - "Jwt": { - "Jwt": { - "Authority": "http://identity:80", - "RequireHttpsMetadata": false, - "MetadataAddress": "http://identity:80/.well-known/openid-configuration" - } } } diff --git a/src/ApiGateway/src/appsettings.json b/src/ApiGateway/src/appsettings.json index 713022d..069b622 100644 --- a/src/ApiGateway/src/appsettings.json +++ b/src/ApiGateway/src/appsettings.json @@ -48,7 +48,7 @@ "identity": { "destinations": { "destination1": { - "address": "https://localhost:5005" + "address": "http://localhost:6005" } } }, @@ -68,12 +68,5 @@ } } }, - "Jwt": { - "Jwt": { - "Authority": "http://localhost:6005", - "RequireHttpsMetadata": false, - "MetadataAddress": "http://localhost:6005/.well-known/openid-configuration" - } - }, "AllowedHosts": "*" } diff --git a/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs b/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs index 3691af9..ae75056 100644 --- a/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs +++ b/src/Services/Identity/src/Identity/Extensions/Infrastructure/InfrastructureExtensions.cs @@ -27,6 +27,7 @@ using Serilog; namespace Identity.Extensions.Infrastructure; using Configurations; +using Microsoft.AspNetCore.HttpOverrides; public static class InfrastructureExtensions { @@ -89,6 +90,15 @@ public static class InfrastructureExtensions var env = app.Environment; var appOptions = app.GetOptions(nameof(AppOptions)); + var forwardHeaderOptions = new ForwardedHeadersOptions + { + ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto + }; + forwardHeaderOptions.KnownNetworks.Clear(); + forwardHeaderOptions.KnownProxies.Clear(); + + app.UseForwardedHeaders(forwardHeaderOptions); + app.UseProblemDetails(); app.UseSerilogRequestLogging(options => {