|
5 | 5 | using System.Net.Http; |
6 | 6 | using System.Text.Json; |
7 | 7 | using System.Threading.Tasks; |
| 8 | + using Microsoft.AspNetCore.Http; |
8 | 9 | using Microsoft.Extensions.Logging; |
9 | 10 | using ServiceBus.Management.Infrastructure.Settings; |
10 | 11 |
|
11 | | - class RemotePlatformConnectionDetailsProvider(Settings settings, IHttpClientFactory clientFactory, ILogger<RemotePlatformConnectionDetailsProvider> logger) |
| 12 | + class RemotePlatformConnectionDetailsProvider(Settings settings, IHttpClientFactory clientFactory, IHttpContextAccessor httpContextAccessor, ILogger<RemotePlatformConnectionDetailsProvider> logger) |
12 | 13 | : IProvidePlatformConnectionDetails |
13 | 14 | { |
14 | | - public Task ProvideConnectionDetails(PlatformConnectionDetails connection) => |
15 | | - Task.WhenAll( |
| 15 | + public Task ProvideConnectionDetails(PlatformConnectionDetails connection) |
| 16 | + { |
| 17 | + var authorizationHeader = httpContextAccessor.HttpContext?.Request.Headers.Authorization.ToString(); |
| 18 | + |
| 19 | + return Task.WhenAll( |
16 | 20 | settings.RemoteInstances |
17 | | - .Select(remote => UpdateFromRemote(remote, connection)) |
| 21 | + .Select(remote => UpdateFromRemote(remote, connection, authorizationHeader)) |
18 | 22 | ); |
| 23 | + } |
19 | 24 |
|
20 | | - async Task UpdateFromRemote(RemoteInstanceSetting remote, PlatformConnectionDetails connection) |
| 25 | + async Task UpdateFromRemote(RemoteInstanceSetting remote, PlatformConnectionDetails connection, string authorizationHeader) |
21 | 26 | { |
22 | 27 | var client = clientFactory.CreateClient(remote.InstanceId); |
23 | 28 | try |
24 | 29 | { |
25 | | - await using var stream = await client.GetStreamAsync("/api/connection"); |
| 30 | + var request = new HttpRequestMessage(HttpMethod.Get, "/api/connection"); |
| 31 | + var hasAuth = !string.IsNullOrEmpty(authorizationHeader); |
| 32 | + |
| 33 | + if (hasAuth) |
| 34 | + { |
| 35 | + request.Headers.TryAddWithoutValidation("Authorization", authorizationHeader); |
| 36 | + } |
| 37 | + |
| 38 | + var response = await client.SendAsync(request); |
| 39 | + response.EnsureSuccessStatusCode(); |
| 40 | + |
| 41 | + await using var stream = await response.Content.ReadAsStreamAsync(); |
26 | 42 | var document = await JsonDocument.ParseAsync(stream); |
27 | 43 | foreach (var property in document.RootElement.EnumerateObject()) |
28 | 44 | { |
|
0 commit comments