Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API updates per review feedback #198

Merged
merged 6 commits into from
Sep 14, 2022
Merged
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
13 changes: 6 additions & 7 deletions docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ var builder = WebApplication.CreateBuilder();
builder.Services.AddControllersWithViews();
builder.Services.AddSystemWebAdapters()
.AddJsonSessionKeySerializer(options => ClassLibrary.SessionUtils.RegisterSessionKeys(options))
.AddRemoteAppClient(remote => remote
.Configure(options =>
{
options.RemoteApp = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = ClassLibrary.SessionUtils.ApiKey;
})
.AddSession());
.AddRemoteAppClient(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration("RemoteAppApiKey");
})
.AddSessionClient();

var app = builder.Build();

Expand Down
5 changes: 2 additions & 3 deletions docs/framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ protected void Application_Start()
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddProxySupport(options => options.UseForwardedHeaders = true)
.AddJsonSessionSerializer(options => ClassLibrary.SessionUtils.RegisterSessionKeys(options.KnownKeys))
.AddRemoteAppServer(remote => remote
.Configure(options => options.ApiKey = ClassLibrary.SessionUtils.ApiKey)
.AddSession());
.AddRemoteAppServer(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddSessionServer();
}
```

Expand Down
12 changes: 5 additions & 7 deletions docs/remote-app-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ To setup the ASP.NET app to be able to receive requests from the ASP.NET Core ap

```CSharp
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddRemoteApp(options =>
.AddRemoteAppServer(options =>
{
// ApiKey is a string representing a GUID
options.ApiKey = "00000000-0000-0000-0000-000000000000";
options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"];
});
```

Expand All @@ -29,12 +29,10 @@ To setup the ASP.NET Core app to be able to send requests to the ASP.NET app, yo

```CSharp
builder.Services.AddSystemWebAdapters()
.AddRemoteApp(options =>
.AddRemoteAppClient(options =>
{
options.RemoteAppUrl = new(builder.Configuration["http://URL-for-the-ASPNet-app"]);

// ApiKey is a string representing a GUID
options.ApiKey = "00000000-0000-0000-0000-000000000000";
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration("RemoteAppApiKey");
});
```

Expand Down
9 changes: 3 additions & 6 deletions docs/remote-authentication/remote-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddProxySupport(options => options.UseForwardedHeaders = true)
.AddRemoteApp(options =>
{
// ApiKey is a string representing a GUID
options.ApiKey = "00000000-0000-0000-0000-000000000000";
options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"];
})
.AddRemoteAppAuthentication();
```
Expand All @@ -31,10 +30,8 @@ Next, the ASP.NET Core app needs to be configured to enable the authentication h
builder.Services.AddSystemWebAdapters()
.AddRemoteApp(options =>
{
options.RemoteAppUrl = new(builder.Configuration["http://URL-for-the-ASPNet-app"]);

// ApiKey is a string representing a GUID
options.ApiKey = "00000000-0000-0000-0000-000000000000";
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration("RemoteAppApiKey");
})
.AddRemoteAppAuthentication(true);
```
Expand Down
29 changes: 14 additions & 15 deletions docs/session-state/remote-session.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ Configuration for ASP.NET Core involves calling `AddRemoteAppSession` and `AddJs

```csharp
builder.Services.AddSystemWebAdapters()
.AddRemoteApp(options =>
.AddJsonSessionSerializer(options =>
{
// Serialization/deserialization requires each session key to be registered to a type
options.RegisterKey<int>("test-value");
options.RegisterKey<SessionDemoModel>("SampleSessionItem");
})
.AddRemoteAppClient(options =>
{
// Provide the URL for the remote app that has enabled session querying
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);

// Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
// ApiKey is a string representing a GUID
options.ApiKey = "00000000-0000-0000-0000-000000000000";
options.ApiKey = builder.Configuration("RemoteAppApiKey");
})
.AddRemoteAppSession()
.AddJsonSessionSerializer(options =>
{
// Serialization/deserialization requires each session key to be registered to a type
options.RegisterKey<int>("test-value");
options.RegisterKey<SessionDemoModel>("SampleSessionItem");
});
.AddSessionClient();
```

Session support requires additional work for the ASP.NET Core pipeline, and is not turned on by default. It can be configured on a per-route basis via ASP.NET Core metadata.
Expand All @@ -62,16 +61,16 @@ The framework equivalent would look like the following change in `Global.asax.cs

```csharp
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
// Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
// ApiKey is a string representing a GUID
.AddRemoteApp(options => options.ApiKey = "00000000-0000-0000-0000-000000000000")
.AddRemoteAppSession()
.AddJsonSessionSerializer(options =>
{
// Serialization/deserialization requires each session key to be registered to a type
options.RegisterKey<int>("test-value");
options.RegisterKey<SessionDemoModel>("SampleSessionItem");
});
})
// Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
// ApiKey is a string representing a GUID
.AddRemoteAppServer(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddSessionServer();
```

# Protocol
Expand Down
7 changes: 3 additions & 4 deletions samples/MvcApp/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ protected void Application_Start()
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddProxySupport(options => options.UseForwardedHeaders = true)
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys))
.AddRemoteAppServer(remote => remote
.Configure(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddAuthentication()
.AddSession());
.AddRemoteAppServer(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddAuthenticationServer()
.AddSessionServer();
}
}
}
17 changes: 8 additions & 9 deletions samples/MvcCoreApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSystemWebAdapters()
.AddRemoteAppClient(remote => remote
.Configure(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})
.AddAuthentication(true)
.AddSession())
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys));
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys))
.AddRemoteAppClient(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})
.AddAuthenticationClient(true)
.AddSessionClient();

var app = builder.Build();

Expand Down
10 changes: 2 additions & 8 deletions samples/RemoteAuth/Bearer/RemoteBearer/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ protected void Application_Start()
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddRemoteAppServer(remote => remote
.Configure(options =>
{
// A real application would not hard code this, but load it
// securely from environment or configuration
options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"];
})
.AddAuthentication());
.AddRemoteAppServer(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddAuthenticationServer();
}
}
}
23 changes: 11 additions & 12 deletions samples/RemoteAuth/Bearer/RemoteBearerCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
builder.Services.AddControllers();

builder.Services.AddSystemWebAdapters()
.AddRemoteAppClient(remote => remote
.Configure(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})

// This registers the remote app authentication handler. The boolean argument indicates whether remote app auth
// should be the default scheme. If it is set to false, HTTP requests to authenticate will only be made for
// endpoints that actually need that behavior, but it is then necessary to annotate endpoints requiring remote app
// auth with [Authorize(AuthenticationSchemes = RemoteAppAuthenticationDefaults.AuthenticationScheme)] or something similar.
.AddAuthentication(isDefaultScheme: true));
.AddRemoteAppClient(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})

// This registers the remote app authentication handler. The boolean argument indicates whether remote app auth
// should be the default scheme. If it is set to false, HTTP requests to authenticate will only be made for
// endpoints that actually need that behavior, but it is then necessary to annotate endpoints requiring remote app
// auth with [Authorize(AuthenticationSchemes = RemoteAppAuthenticationDefaults.AuthenticationScheme)] or something similar.
.AddAuthenticationClient(isDefaultScheme: true);

var app = builder.Build();

Expand Down
5 changes: 2 additions & 3 deletions samples/RemoteAuth/Forms/FormsAuth/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ void Application_Start(object sender, EventArgs e)

SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddProxySupport(options => options.UseForwardedHeaders = true)
.AddRemoteAppServer(remote => remote
.Configure(options =>
.AddRemoteAppServer(options =>
{
options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"];
})
.AddAuthentication());
.AddAuthenticationServer();
}
}
}
13 changes: 6 additions & 7 deletions samples/RemoteAuth/Forms/FormsAuthCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

// Add System.Web adapter services, including registering remote app authentication
builder.Services.AddSystemWebAdapters()
.AddRemoteAppClient(remote => remote
.Configure(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})
.AddAuthentication(true));
.AddRemoteAppClient(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})
.AddAuthenticationClient(true);

var app = builder.Build();

Expand Down
5 changes: 2 additions & 3 deletions samples/RemoteAuth/OIDC/OIDCAuth/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ protected void Application_Start()

SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
.AddProxySupport(options => options.UseForwardedHeaders = true)
.AddRemoteAppServer(remote => remote
.Configure(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddAuthentication());
.AddRemoteAppServer(options => options.ApiKey = ConfigurationManager.AppSettings["RemoteAppApiKey"])
.AddAuthenticationServer();

}
}
Expand Down
13 changes: 6 additions & 7 deletions samples/RemoteAuth/OIDC/OIDCAuthCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSystemWebAdapters()
.AddRemoteAppClient(remote => remote
.Configure(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})
.AddAuthentication(true));
.AddRemoteAppClient(options =>
{
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
options.ApiKey = builder.Configuration["RemoteAppApiKey"];
})
.AddAuthenticationClient(true);

builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static AuthenticationBuilder AddRemoteAppAuthentication(this Authenticati
/// </summary>
/// <param name="isDefaultScheme">Specifies whether the remote authentication scheme should be the default authentication scheme. If false, remote authentication will only be used for endpoints specifically requiring the remote authentication scheme.</param>
/// <param name="configureOptions">Configuration options for the remote authentication handler.</param>
public static ISystemWebAdapterRemoteClientAppBuilder AddAuthentication(this ISystemWebAdapterRemoteClientAppBuilder builder, bool isDefaultScheme, Action<RemoteAppAuthenticationClientOptions>? configureOptions = null)
public static ISystemWebAdapterRemoteClientAppBuilder AddAuthenticationClient(this ISystemWebAdapterRemoteClientAppBuilder builder, bool isDefaultScheme, Action<RemoteAppAuthenticationClientOptions>? configureOptions = null)
{
if (builder is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class RemoteAppClientExtensions
/// Add configuration for connecting to a remote app for System.Web extensions that require a remote
/// ASP.NET app such as remote app authentication or remote app session sharing.
/// </summary>
public static ISystemWebAdapterBuilder AddRemoteAppClient(this ISystemWebAdapterBuilder builder, Action<ISystemWebAdapterRemoteClientAppBuilder> configure)
public static ISystemWebAdapterRemoteClientAppBuilder AddRemoteAppClient(this ISystemWebAdapterBuilder builder, Action<RemoteAppClientOptions> configure)
{
if (builder is null)
{
Expand All @@ -27,6 +27,7 @@ public static ISystemWebAdapterBuilder AddRemoteAppClient(this ISystemWebAdapter
}

builder.Services.AddOptions<RemoteAppClientOptions>()
.Configure(configure)
.ValidateDataAnnotations();

builder.Services.AddHttpClient(RemoteConstants.HttpClientName)
Expand All @@ -50,27 +51,7 @@ public static ISystemWebAdapterBuilder AddRemoteAppClient(this ISystemWebAdapter
client.DefaultRequestHeaders.Add(options.ApiKeyHeader, options.ApiKey);
});

configure(new Builder(builder.Services));

return builder;
}

public static ISystemWebAdapterRemoteClientAppBuilder Configure(this ISystemWebAdapterRemoteClientAppBuilder builder, Action<RemoteAppClientOptions> configure)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

builder.Services.AddOptions<RemoteAppClientOptions>()
.Configure(configure);

return builder;
return new Builder(builder.Services);
}

private class Builder : ISystemWebAdapterRemoteClientAppBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.DependencyInjection;

public static class RemoteAppSessionStateExtensions
{
public static ISystemWebAdapterRemoteClientAppBuilder AddSession(this ISystemWebAdapterRemoteClientAppBuilder builder, Action<RemoteAppSessionStateClientOptions>? configure = null)
public static ISystemWebAdapterRemoteClientAppBuilder AddSessionClient(this ISystemWebAdapterRemoteClientAppBuilder builder, Action<RemoteAppSessionStateClientOptions>? configure = null)
{
if (builder is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class RemoteAppAuthenticationExtensions
/// <param name="builder">The System.Web adapter builder to modify.</param>
/// <param name="configure">Configuration to use when registering the remote authentication module.</param>
/// <returns>The System.Web adapter builder updated to include the remote authentication module.</returns>
public static ISystemWebAdapterRemoteServerAppBuilder AddAuthentication(this ISystemWebAdapterRemoteServerAppBuilder builder, Action<RemoteAppAuthenticationServerOptions>? configure = null)
public static ISystemWebAdapterRemoteServerAppBuilder AddAuthenticationServer(this ISystemWebAdapterRemoteServerAppBuilder builder, Action<RemoteAppAuthenticationServerOptions>? configure = null)
{
if (builder is null)
{
Expand Down
Loading