-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathRemoteAppClientExtensions.cs
66 lines (54 loc) · 2.27 KB
/
RemoteAppClientExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Net.Http;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.Extensions.Options;
namespace Microsoft.Extensions.DependencyInjection;
public static class RemoteAppClientExtensions
{
/// <summary>
/// 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 ISystemWebAdapterRemoteClientAppBuilder AddRemoteAppClient(this ISystemWebAdapterBuilder 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)
.ValidateDataAnnotations();
builder.Services.AddHttpClient(RemoteConstants.HttpClientName)
.ConfigurePrimaryHttpMessageHandler(sp =>
{
var options = sp.GetRequiredService<IOptions<RemoteAppClientOptions>>().Value;
if (options.BackchannelHandler is { } handler)
{
return handler;
}
// Disable cookies in the HTTP client because the service will manage the cookie header directly
return new HttpClientHandler { UseCookies = false, AllowAutoRedirect = false };
})
.ConfigureHttpClient((sp, client) =>
{
var options = sp.GetRequiredService<IOptions<RemoteAppClientOptions>>().Value;
client.BaseAddress = options.RemoteAppUrl;
client.DefaultRequestHeaders.Add(options.ApiKeyHeader, options.ApiKey);
});
return new Builder(builder.Services);
}
private class Builder : ISystemWebAdapterRemoteClientAppBuilder
{
public Builder(IServiceCollection services)
{
Services = services;
}
public IServiceCollection Services { get; }
}
}