Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="IdentityModel.AspNetCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.4" />
<PackageReference Include="ProxyKit" Version="2.3.4" />
<PackageReference Include="Microsoft.ReverseProxy" Version="1.0.0-preview.9.21116.1" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 13 additions & 16 deletions BlazorWasmCookieAuth/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<IProxyHttpClientFactory, UserAccessTokenProxyHttpClientFactory>();
services.AddReverseProxy()
.LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -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");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
25 changes: 24 additions & 1 deletion BlazorWasmCookieAuth/Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
}
}