From b6ae64a93c0b70440f69eff4d82a72ec93787d12 Mon Sep 17 00:00:00 2001 From: Bernd Hirschmann Date: Wed, 17 Mar 2021 19:58:37 +0100 Subject: [PATCH 1/2] use YARP instead of ProxyKit --- .../AuthenticatedProxyHttpClientFactory.cs | 37 +++++++++++++++++++ .../Server/BlazorWasmCookieAuth.Server.csproj | 2 +- BlazorWasmCookieAuth/Server/Startup.cs | 29 +++++++-------- BlazorWasmCookieAuth/Server/appsettings.json | 25 ++++++++++++- 4 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs diff --git a/BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs b/BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs new file mode 100644 index 0000000..7e9efaa --- /dev/null +++ b/BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs @@ -0,0 +1,37 @@ +using IdentityModel.AspNetCore.AccessTokenManagement; +using Microsoft.ReverseProxy.Service.Proxy.Infrastructure; +using System.Net; +using System.Net.Http; + +namespace BlazorWasmCookieAuth.Server +{ + public class AuthenticatedProxyHttpClientFactory : IProxyHttpClientFactory + { + private readonly UserAccessTokenHandler _userAccessTokenHandler; + + public AuthenticatedProxyHttpClientFactory(UserAccessTokenHandler userAccessTokenHandler) + { + _userAccessTokenHandler = userAccessTokenHandler; + + var socketsHttpHandler = new SocketsHttpHandler + { + UseProxy = false, + AllowAutoRedirect = false, + AutomaticDecompression = DecompressionMethods.None, + UseCookies = false + }; + + _userAccessTokenHandler.InnerHandler = socketsHttpHandler; + } + + public HttpMessageInvoker CreateClient(ProxyHttpClientContext context) + { + if (context.OldClient != null && context.NewOptions == context.OldOptions) + { + return context.OldClient; + } + + return new HttpMessageInvoker(_userAccessTokenHandler, disposeHandler: false); + } + } +} diff --git a/BlazorWasmCookieAuth/Server/BlazorWasmCookieAuth.Server.csproj b/BlazorWasmCookieAuth/Server/BlazorWasmCookieAuth.Server.csproj index 8c2513c..8acfc48 100644 --- a/BlazorWasmCookieAuth/Server/BlazorWasmCookieAuth.Server.csproj +++ b/BlazorWasmCookieAuth/Server/BlazorWasmCookieAuth.Server.csproj @@ -9,7 +9,7 @@ - + diff --git a/BlazorWasmCookieAuth/Server/Startup.cs b/BlazorWasmCookieAuth/Server/Startup.cs index de9299c..ea111f1 100644 --- a/BlazorWasmCookieAuth/Server/Startup.cs +++ b/BlazorWasmCookieAuth/Server/Startup.cs @@ -5,17 +5,25 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Tokens; -using ProxyKit; +using Microsoft.ReverseProxy.Service.Proxy.Infrastructure; using System.IdentityModel.Tokens.Jwt; namespace BlazorWasmCookieAuth.Server { public class Startup { + public IConfiguration Configuration { get; } + + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) @@ -58,12 +66,9 @@ public void ConfigureServices(IServiceCollection services) }); services.AddAccessTokenManagement(); - services.AddProxy((clientBuilder) => - { - // adds the access token to all forwarded requests, - // and refreshes the access token if needed. - clientBuilder.AddUserAccessTokenHandler(); - }); + services.AddSingleton(); + services.AddReverseProxy() + .LoadFromConfig(Configuration.GetSection("ReverseProxy")); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -83,17 +88,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseAuthentication(); app.UseAuthorization(); - app.Map("/api", api => - { - api.RunProxy(async context => - { - var forwardContext = context.ForwardTo("https://localhost:5101"); - return await forwardContext.Send(); - }); - }); - app.UseEndpoints(endpoints => { + endpoints.MapReverseProxy(); endpoints.MapControllers(); endpoints.MapFallbackToFile("index.html"); }); diff --git a/BlazorWasmCookieAuth/Server/appsettings.json b/BlazorWasmCookieAuth/Server/appsettings.json index d9d9a9b..66fcad5 100644 --- a/BlazorWasmCookieAuth/Server/appsettings.json +++ b/BlazorWasmCookieAuth/Server/appsettings.json @@ -6,5 +6,28 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ReverseProxy": { + "Routes": [ + { + "RouteId": "apiRoute", + "ClusterId": "apiCluster", + "Match": { + "Path": "api/{**catch-all}" + }, + "Transforms": [ + { "PathRemovePrefix": "/api" } + ] + } + ], + "Clusters": { + "apiCluster": { + "Destinations": { + "apiDestination": { + "Address": "https://localhost:5101" + } + } + } + } + } } From a909bf6f2fafa614ba521ba16bb5be6d6a3e1424 Mon Sep 17 00:00:00 2001 From: Bernd Hirschmann Date: Thu, 18 Mar 2021 16:06:22 +0100 Subject: [PATCH 2/2] rename factory --- BlazorWasmCookieAuth/Server/Startup.cs | 2 +- ...entFactory.cs => UserAccessTokenProxyHttpClientFactory.cs} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename BlazorWasmCookieAuth/Server/{AuthenticatedProxyHttpClientFactory.cs => UserAccessTokenProxyHttpClientFactory.cs} (85%) diff --git a/BlazorWasmCookieAuth/Server/Startup.cs b/BlazorWasmCookieAuth/Server/Startup.cs index ea111f1..32fee5b 100644 --- a/BlazorWasmCookieAuth/Server/Startup.cs +++ b/BlazorWasmCookieAuth/Server/Startup.cs @@ -66,7 +66,7 @@ public void ConfigureServices(IServiceCollection services) }); services.AddAccessTokenManagement(); - services.AddSingleton(); + services.AddSingleton(); services.AddReverseProxy() .LoadFromConfig(Configuration.GetSection("ReverseProxy")); } diff --git a/BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs b/BlazorWasmCookieAuth/Server/UserAccessTokenProxyHttpClientFactory.cs similarity index 85% rename from BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs rename to BlazorWasmCookieAuth/Server/UserAccessTokenProxyHttpClientFactory.cs index 7e9efaa..26e9a60 100644 --- a/BlazorWasmCookieAuth/Server/AuthenticatedProxyHttpClientFactory.cs +++ b/BlazorWasmCookieAuth/Server/UserAccessTokenProxyHttpClientFactory.cs @@ -5,11 +5,11 @@ namespace BlazorWasmCookieAuth.Server { - public class AuthenticatedProxyHttpClientFactory : IProxyHttpClientFactory + public class UserAccessTokenProxyHttpClientFactory : IProxyHttpClientFactory { private readonly UserAccessTokenHandler _userAccessTokenHandler; - public AuthenticatedProxyHttpClientFactory(UserAccessTokenHandler userAccessTokenHandler) + public UserAccessTokenProxyHttpClientFactory(UserAccessTokenHandler userAccessTokenHandler) { _userAccessTokenHandler = userAccessTokenHandler;