Skip to content

Commit 55643e1

Browse files
authored
Update remote app builder pattern (#120)
1 parent 3c99c6d commit 55643e1

File tree

39 files changed

+459
-208
lines changed

39 files changed

+459
-208
lines changed

docs/core.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ var builder = WebApplication.CreateBuilder();
1010
// Add services to the container.
1111
builder.Services.AddControllersWithViews();
1212
builder.Services.AddSystemWebAdapters()
13+
<<<<<<< HEAD
14+
.AddJsonSessionKeySerializer(options => ClassLibrary.SessionUtils.RegisterSessionKeys(options))
15+
.AddRemoteClientApp(remote => remote
16+
.Configure(options =>
17+
{
18+
options.RemoteApp = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
19+
options.ApiKey = ClassLibrary.SessionUtils.ApiKey;
20+
})
21+
.AddSession();
22+
=======
1323
.AddRemoteApp(options =>
1424
{
1525
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
1626
options.ApiKey = ClassLibrary.SessionUtils.ApiKey;
1727
})
1828
.AddRemoteAppSession()
1929
.AddJsonSessionSerializer(options => ClassLibrary.SessionUtils.RegisterSessionKeys(options));
30+
>>>>>>> origin/main
2031

2132
var app = builder.Build();
2233

docs/framework.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ protected void Application_Start()
2828

2929
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
3030
.AddProxySupport(options => options.UseForwardedHeaders = true)
31+
<<<<<<< HEAD
32+
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys))
33+
.AddRemoteAppServer(remote => remote
34+
.Configure(options => options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey)
35+
.AddSession());
36+
=======
3137
.AddRemoteApp(options => options.ApiKey = ClassLibrary.SessionUtils.ApiKey)
3238
.AddRemoteAppSession()
3339
.AddJsonSessionSerializer(options => ClassLibrary.SessionUtils.RegisterSessionKeys(options.KnownKeys));
40+
>>>>>>> origin/main
3441
}
3542
```
3643

docs/remote-authentication/remote-authentication.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ There are just a few small code changes needed to enable remote authentication i
88

99
### ASP.NET app configuration
1010

11+
<<<<<<< HEAD
12+
First, the ASP.NET app needs to be configured to add the authentication endpoint. This is done by calling the `AddRemoteAppServer` extension method on the `ISystemWebAdapterBuilder`:
13+
14+
```CSharp
15+
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
16+
.AddRemoteAppServer(remote => remote
17+
.Configure(options => options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey)
18+
.AddAuthentication());
19+
=======
1120
First, the ASP.NET app needs to be configured to add the authentication endpoint. This is done by calling the `AddRemoteApp` extension method on the `ISystemWebAdapterBuilder` to configure receiving remote calls, and by calling `AddRemoteAuthentication` to set up the HTTP module that will watch for requests to the authentication endpoint. Note that remote authentication scenarios typically want to add proxy support, as well, so that any auth-related redirects will correctly route to the ASP.NET Core app rather than the ASP.NET one.
1221

1322
```CSharp
@@ -18,12 +27,26 @@ SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
1827
options.ApiKey = "MySecretKey";
1928
})
2029
.AddRemoteAppAuthentication();
30+
>>>>>>> origin/main
2131
```
2232

2333
In the options configuration method passed to the `AddRemoteApp` call, you must specify an API key which is used to secure the endpoint so that only trusted callers can make requests to it (this same API key will be provided to the ASP.NET Core app when it is configured).
2434

2535
### ASP.NET Core app configuration
2636

37+
<<<<<<< HEAD
38+
Next, the ASP.NET Core app needs to be configured to enable the authentication handler that will authenticate users by making an HTTP request to the ASP.NET app. This is done by calling `AddRemoteAppClient` when registering System.Web adapters services:
39+
40+
```CSharp
41+
builder.Services.AddSystemWebAdapters()
42+
.AddRemoteAppClient(remote => remote
43+
.Configure(options =>
44+
{
45+
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
46+
options.ApiKey = "MySecretKey";
47+
})
48+
.AddAuthentication(true));
49+
=======
2750
Next, the ASP.NET Core app needs to be configured to enable the authentication handler that will authenticate users by making an HTTP request to the ASP.NET app. Again, this is done by calling `AddRemoteApp` and `AddRemoteAppAuthentication` when registering System.Web adapters services:
2851

2952
```CSharp
@@ -34,6 +57,7 @@ builder.Services.AddSystemWebAdapters()
3457
options.ApiKey = "MySecretKey";
3558
})
3659
.AddRemoteAppAuthentication(true);
60+
>>>>>>> origin/main
3761
```
3862

3963
The `AddRemoteApp` call is used to configure the remote app's URL and the shared secret API key.

docs/session-state/remote-session.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ Configuration for ASP.NET Core would look similar to the following:
2525

2626
```csharp
2727
builder.Services.AddSystemWebAdapters()
28+
<<<<<<< HEAD
29+
.AddRemoteAppClient(remote => remote
30+
.Configure(options =>
31+
{
32+
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
33+
options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey;
34+
})
35+
.AddSession())
36+
=======
2837
.AddRemoteApp(options =>
2938
{
3039
// Provide the URL for the remote app that has enabled session querying
@@ -34,6 +43,7 @@ builder.Services.AddSystemWebAdapters()
3443
options.ApiKey = "strong-api-key";
3544
})
3645
.AddRemoteAppSession()
46+
>>>>>>> origin/main
3747
.AddJsonSessionSerializer(options =>
3848
{
3949
// Serialization/deserialization requires each session key to be registered to a type
@@ -65,15 +75,26 @@ The framework equivalent would look like the following change in `Global.asax.cs
6575

6676
```csharp
6777
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
78+
<<<<<<< HEAD
79+
.AddProxySupport(options => options.UseForwardedHeaders = true)
80+
=======
6881
// Provide a strong API key that will be used to authenticate the request on the remote app for querying the session
6982
.AddRemoteApp(options => options.ApiKey = "strong-api-key")
7083
.AddRemoteAppSession()
84+
>>>>>>> origin/main
7185
.AddJsonSessionSerializer(options =>
7286
{
7387
// Serialization/deserialization requires each session key to be registered to a type
7488
options.RegisterKey<int>("test-value");
7589
options.RegisterKey<SessionDemoModel>("SampleSessionItem");
90+
<<<<<<< HEAD
91+
})
92+
.AddRemoteAppServer(remote => remote
93+
.Configure(options => options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey)
94+
.AddSession());
95+
=======
7696
});
97+
>>>>>>> origin/main
7798
```
7899
# Protocol
79100

samples/MvcApp/Global.asax.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ protected void Application_Start()
1818

1919
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
2020
.AddProxySupport(options => options.UseForwardedHeaders = true)
21-
.AddRemoteApp(options =>
22-
{
23-
options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey;
24-
})
25-
.AddRemoteAppSession()
2621
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys))
27-
.AddRemoteAppAuthentication();
22+
.AddRemoteAppServer(remote => remote
23+
.Configure(options => options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey)
24+
.AddAuthentication()
25+
.AddSession());
2826
}
2927
}
3028
}

samples/MvcCoreApp/Program.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
// Add services to the container.
88
builder.Services.AddControllersWithViews();
99
builder.Services.AddSystemWebAdapters()
10-
.AddRemoteApp(options =>
11-
{
12-
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
13-
options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey;
14-
})
15-
.AddRemoteAppSession()
16-
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys))
17-
.AddRemoteAppAuthentication(true);
10+
.AddRemoteAppClient(remote => remote
11+
.Configure(options =>
12+
{
13+
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
14+
options.ApiKey = ClassLibrary.RemoteServiceUtils.ApiKey;
15+
})
16+
.AddAuthentication(true)
17+
.AddSession())
18+
.AddJsonSessionSerializer(options => ClassLibrary.RemoteServiceUtils.RegisterSessionKeys(options.KnownKeys));
1819

1920
var app = builder.Build();
2021

samples/RemoteAuth/Bearer/RemoteBearer/Global.asax.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ protected void Application_Start()
1212
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
1313

1414
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
15-
.AddRemoteApp(options =>
16-
{
17-
// A real application would not hard code this, but load it
18-
// securely from environment or configuration
19-
options.ApiKey = "TopSecretString";
20-
})
21-
.AddRemoteAppAuthentication();
15+
.AddRemoteAppServer(remote => remote
16+
.Configure(options =>
17+
{
18+
// A real application would not hard code this, but load it
19+
// securely from environment or configuration
20+
options.ApiKey = "TopSecretString";
21+
})
22+
.AddAuthentication());
2223
}
2324
}
2425
}

samples/RemoteAuth/Bearer/RemoteBearerCore/Program.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
builder.Services.AddControllers();
77

88
builder.Services.AddSystemWebAdapters()
9-
.AddRemoteApp(options =>
10-
{
11-
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
12-
13-
// A real application would not hard code this, but load it
14-
// securely from environment or configuration
15-
options.ApiKey = "TopSecretString";
16-
})
17-
// This registers the remote app authentication handler. The boolean argument indicates whether remote app auth
18-
// should be the default scheme. If it is set to false, HTTP requests to authenticate will only be made for
19-
// endpoints that actually need that behavior, but it is then necessary to annotate endpoints requiring remote app
20-
// auth with [Authorize(AuthenticationSchemes = RemoteAppAuthenticationDefaults.AuthenticationScheme)] or something similar.
21-
.AddRemoteAppAuthentication(isDefaultScheme: true);
9+
.AddRemoteAppClient(remote => remote
10+
.Configure(options =>
11+
{
12+
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
13+
14+
// A real application would not hard code this, but load it
15+
// securely from environment or configuration
16+
options.ApiKey = "TopSecretString";
17+
})
18+
19+
// This registers the remote app authentication handler. The boolean argument indicates whether remote app auth
20+
// should be the default scheme. If it is set to false, HTTP requests to authenticate will only be made for
21+
// endpoints that actually need that behavior, but it is then necessary to annotate endpoints requiring remote app
22+
// auth with [Authorize(AuthenticationSchemes = RemoteAppAuthenticationDefaults.AuthenticationScheme)] or something similar.
23+
.AddAuthentication(isDefaultScheme: true));
2224

2325
var app = builder.Build();
2426

samples/RemoteAuth/Forms/FormsAuth/Global.asax.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ void Application_Start(object sender, EventArgs e)
1616

1717
SystemWebAdapterConfiguration.AddSystemWebAdapters(this)
1818
.AddProxySupport(options => options.UseForwardedHeaders = true)
19-
.AddRemoteApp(options =>
20-
{
21-
options.ApiKey = "FormsAuthSampleKey";
22-
})
23-
.AddRemoteAppAuthentication();
19+
.AddRemoteAppServer(remote => remote
20+
.Configure(options =>
21+
{
22+
options.ApiKey = "FormsAuthSampleKey";
23+
})
24+
.AddAuthentication());
2425
}
2526
}
2627
}

samples/RemoteAuth/Forms/FormsAuthCore/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Microsoft.AspNetCore.SystemWebAdapters;
21
var builder = WebApplication.CreateBuilder(args);
32

43
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
@@ -8,12 +7,13 @@
87

98
// Add System.Web adapter services, including registering remote app authentication
109
builder.Services.AddSystemWebAdapters()
11-
.AddRemoteApp(options =>
12-
{
13-
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
14-
options.ApiKey = "FormsAuthSampleKey";
15-
})
16-
.AddRemoteAppAuthentication(true);
10+
.AddRemoteAppClient(remote => remote
11+
.Configure(options =>
12+
{
13+
options.RemoteAppUrl = new(builder.Configuration["ReverseProxy:Clusters:fallbackCluster:Destinations:fallbackApp:Address"]);
14+
options.ApiKey = "FormsAuthSampleKey";
15+
})
16+
.AddAuthentication(true));
1717

1818
var app = builder.Build();
1919

0 commit comments

Comments
 (0)