Skip to content
Open
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
22 changes: 22 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.200",
"version": "7.0",
"rollForward": "latestFeature"
}
}
2 changes: 2 additions & 0 deletions samples/AspNetMultipleSpaYarp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AspNetCore.SpaYarp.Extensions;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand Down
2 changes: 2 additions & 0 deletions samples/Net6Startup/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AspNetCore.SpaYarp.Extensions;

public class Startup
{
public Startup(IConfiguration configuration)
Expand Down
2 changes: 1 addition & 1 deletion src/AspNetCore.SpaYarp/AspNetCore.SpaYarp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Bernd Hirschmann</Authors>
<Company>Guid.New GmbH</Company>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -16,11 +15,11 @@ public static class IEndpointRouteBuilderExtensions
/// Adds a "catch-all" route endpoint to the <see cref="IEndpointRouteBuilder"/> that forwards all requests to the SPA dev server.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the route endpoint to.</param>
/// <returns>The <see cref="IEndpointRouteBuilder"/>.</returns>
public static IEndpointRouteBuilder MapSpaYarp(this IEndpointRouteBuilder endpoints)
/// <returns>The <see cref="IEndpointConventionBuilder"/>.</returns>
public static IEndpointConventionBuilder? MapSpaYarp(this IEndpointRouteBuilder endpoints)
{
var spaOptions = endpoints.ServiceProvider.GetRequiredService<IOptions<SpaDevelopmentServerOptions>>().Value;
return MapSpaYarp(endpoints, spaOptions.PublicPath, spaOptions.ClientUrl);
return endpoints.MapSpaYarp(spaOptions.PublicPath, spaOptions.ClientUrl);
}

/// <summary>
Expand All @@ -31,22 +30,20 @@ public static IEndpointRouteBuilder MapSpaYarp(this IEndpointRouteBuilder endpoi
/// <param name="clientUrl">The Url of the dev server to proxy to</param>
/// <param name="policyName">The auth policy name to add to the mapping</param>
/// <returns>The <see cref="IEndpointRouteBuilder"/>.</returns>
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<SpaProxyLaunchManager>();

if (spaProxyLaunchManager == null)
{
return endpoints;
return null;
}

// configure the proxy
var forwarder = endpoints.ServiceProvider.GetRequiredService<IHttpForwarder>();

// 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,
Expand All @@ -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;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.AspNetCore.Builder;
using AspNetCore.SpaYarp.Extensions;

namespace Microsoft.AspNetCore.Builder;

public static class WebApplicationExtensions
{
Expand Down