diff --git a/Readme.md b/Readme.md index 37c62b4..5cb9e01 100644 --- a/Readme.md +++ b/Readme.md @@ -185,6 +185,28 @@ app.MapSpaYarp("two", "https://localhost:44479"); ``` +### Configure route conventions + +MapSpaYarp returns the `IEndpointConventionBuilder`, which allows to add authorization policies and other route conventions. + +Here is an example which adds required authorization: + +```cs +app.UseSpaYarpMiddleware(); +app.MapSpaYarp().RequireAuthorization(); +//app.MapSpaYarp().RequireAuthorization("MySpaYarpPolicy"); + +``` + +Here is an example which allows anonymous access (e.g. if authorization is required by default for all Endpoints): + +```cs +app.UseSpaYarpMiddleware(); +app.MapSpaYarp().AllowAnonymousAccess(); + +``` + + ## Migrate from SpaProxy This guide assumes that you are using the default ASP.NET Core with Angular Template (but should work the same for other frameworks too). diff --git a/global.json b/global.json index 5bf3e71..6af8bc2 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.200", + "version": "7.0", "rollForward": "latestFeature" } } diff --git a/samples/AspNetMultipleSpaYarp/Program.cs b/samples/AspNetMultipleSpaYarp/Program.cs index 99e73cb..f2d018d 100644 --- a/samples/AspNetMultipleSpaYarp/Program.cs +++ b/samples/AspNetMultipleSpaYarp/Program.cs @@ -1,3 +1,5 @@ +using AspNetCore.SpaYarp.Extensions; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. diff --git a/samples/Net6Startup/Startup.cs b/samples/Net6Startup/Startup.cs index 2320095..f0b8b82 100644 --- a/samples/Net6Startup/Startup.cs +++ b/samples/Net6Startup/Startup.cs @@ -1,3 +1,5 @@ +using AspNetCore.SpaYarp.Extensions; + public class Startup { public Startup(IConfiguration configuration) diff --git a/src/AspNetCore.SpaYarp/AspNetCore.SpaYarp.csproj b/src/AspNetCore.SpaYarp/AspNetCore.SpaYarp.csproj index 8cc9912..ea00b74 100644 --- a/src/AspNetCore.SpaYarp/AspNetCore.SpaYarp.csproj +++ b/src/AspNetCore.SpaYarp/AspNetCore.SpaYarp.csproj @@ -12,7 +12,7 @@ MIT Bernd Hirschmann Guid.New GmbH - 2.0.1 + 2.0.2 True Readme.md diff --git a/src/AspNetCore.SpaYarp/Extensions/IEndpointRouteBuilderExtensions.cs b/src/AspNetCore.SpaYarp/Extensions/IEndpointRouteBuilderExtensions.cs index 3cb87df..f6b325d 100644 --- a/src/AspNetCore.SpaYarp/Extensions/IEndpointRouteBuilderExtensions.cs +++ b/src/AspNetCore.SpaYarp/Extensions/IEndpointRouteBuilderExtensions.cs @@ -1,13 +1,12 @@ -using AspNetCore.SpaYarp; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System.Net; -using Microsoft.AspNetCore.Authorization; using Yarp.ReverseProxy.Forwarder; -namespace Microsoft.AspNetCore.Builder; +namespace AspNetCore.SpaYarp.Extensions; public static class IEndpointRouteBuilderExtensions { @@ -16,11 +15,11 @@ public static class IEndpointRouteBuilderExtensions /// Adds a "catch-all" route endpoint to the that forwards all requests to the SPA dev server. /// /// The to add the route endpoint to. - /// The . - public static IEndpointRouteBuilder MapSpaYarp(this IEndpointRouteBuilder endpoints) + /// The . + public static IEndpointConventionBuilder? MapSpaYarp(this IEndpointRouteBuilder endpoints) { var spaOptions = endpoints.ServiceProvider.GetRequiredService>().Value; - return MapSpaYarp(endpoints, spaOptions.PublicPath, spaOptions.ClientUrl); + return endpoints.MapSpaYarp(spaOptions.PublicPath, spaOptions.ClientUrl); } /// @@ -31,22 +30,20 @@ public static IEndpointRouteBuilder MapSpaYarp(this IEndpointRouteBuilder endpoi /// The Url of the dev server to proxy to /// The auth policy name to add to the mapping /// The . - public static IEndpointRouteBuilder MapSpaYarp(this IEndpointRouteBuilder endpoints, string publicPath, - string clientUrl, string? policyName = null) + public static IEndpointConventionBuilder? MapSpaYarp(this IEndpointRouteBuilder endpoints, string publicPath, string clientUrl) { var spaProxyLaunchManager = endpoints.ServiceProvider.GetService(); if (spaProxyLaunchManager == null) { - return endpoints; + return null; } // configure the proxy var forwarder = endpoints.ServiceProvider.GetRequiredService(); // Configure our own HttpMessageInvoker for outbound calls for proxy operations - var httpClient = new HttpMessageInvoker(new SocketsHttpHandler() - { + var httpClient = new HttpMessageInvoker(new SocketsHttpHandler() { UseProxy = false, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None, @@ -66,12 +63,7 @@ public static IEndpointRouteBuilder MapSpaYarp(this IEndpointRouteBuilder endpoi var exception = errorFeature?.Exception; } }); - if (policyName is not null) - { - point.RequireAuthorization(policyName); - } - - return endpoints; + return point; } /// diff --git a/src/AspNetCore.SpaYarp/Extensions/WebApplicationExtensions.cs b/src/AspNetCore.SpaYarp/Extensions/WebApplicationExtensions.cs index 4375d52..6146911 100644 --- a/src/AspNetCore.SpaYarp/Extensions/WebApplicationExtensions.cs +++ b/src/AspNetCore.SpaYarp/Extensions/WebApplicationExtensions.cs @@ -1,4 +1,6 @@ -namespace Microsoft.AspNetCore.Builder; +using AspNetCore.SpaYarp.Extensions; + +namespace Microsoft.AspNetCore.Builder; public static class WebApplicationExtensions {