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..32fee5b 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/UserAccessTokenProxyHttpClientFactory.cs b/BlazorWasmCookieAuth/Server/UserAccessTokenProxyHttpClientFactory.cs
new file mode 100644
index 0000000..26e9a60
--- /dev/null
+++ b/BlazorWasmCookieAuth/Server/UserAccessTokenProxyHttpClientFactory.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 UserAccessTokenProxyHttpClientFactory : IProxyHttpClientFactory
+ {
+ private readonly UserAccessTokenHandler _userAccessTokenHandler;
+
+ public UserAccessTokenProxyHttpClientFactory(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/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"
+ }
+ }
+ }
+ }
+ }
}