berhir / AspNetCore.SpaYarp

An alternative approach to the new ASP.NET Core SPA templates in .NET 6. It uses YARP as proxy to forward requests to the SPA dev server.
MIT License
99 stars 12 forks source link

MapSpaYarp prevents usage of AllowAnonymous #32

Open bve-wd opened 8 months ago

bve-wd commented 8 months ago

We are using Authentication and Authorization in our application and enabled them by default if no authorize attribute is used. In such scenarios, MapSpaProxy hides the access to the route configuration.

I created PR #31 which removes the optional policyName attribute and adds IEndpointConventionBuilder as return value instead. This allows to decide how to handle route conventions similar to when controllers or static files are mapped.

app = builder.Build();
...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
...
app.UseSpaYarpMiddleware();
// allow anonymous access to fallback routes (index.html and angular dev-server assets)
app.MapSpaYarp().AllowAnonymous();

// alternative usage:
app.MapSpaYarp().RequireAuthorization(); // requires the default policy
app.MapSpaYarp().RequireAuthorization("SpaYarpPolicyName"); // requires a custom policy
// mixed setup with authorized and public SPA proxy paths
app.MapSpaYarp("EndpointPath1","https://localhost:7890").RequireAuthorization();
app.MapSpaYarp("EndpointPath2","https://localhost:7880").AllowAnonymous();

As quickfix, I added a custom implementation of the MapSpaYarp method in our project but it would be nice if you can merge it and create a new nuget package version.