-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathAddTokenHandlerS2S.cs
More file actions
77 lines (68 loc) · 3.49 KB
/
AddTokenHandlerS2S.cs
File metadata and controls
77 lines (68 loc) · 3.49 KB
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
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Agents.CopilotStudio.Client;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using Microsoft.Identity.Client.Extensions.Msal;
using Microsoft.Identity.Client;
namespace CopilotStudioClientSample
{
/// <summary>
/// This sample uses an HttpClientHandler to add an authentication token to the request.
/// In this case its using client secret for the request.
/// </summary>
/// <param name="settings">Direct To engine connection settings.</param>
internal class AddTokenHandlerS2S(SampleConnectionSettings settings) : DelegatingHandler(new HttpClientHandler())
{
private static readonly string _keyChainServiceName = "copilot_studio_client_app";
private static readonly string _keyChainAccountName = "copilot_studio_client";
private IConfidentialClientApplication? _confidentialClientApplication;
private string[]? _scopes;
private async Task<AuthenticationResult> AuthenticateAsync(CancellationToken ct = default!)
{
if (_confidentialClientApplication == null)
{
ArgumentNullException.ThrowIfNull(settings);
_scopes = [CopilotClient.ScopeFromSettings(settings)];
_confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(settings.AppClientId)
.WithAuthority(AzureCloudInstance.AzurePublic, settings.TenantId)
.WithClientSecret(settings.AppClientSecret)
.Build();
string currentDir = Path.Combine(AppContext.BaseDirectory, "mcs_client_console");
if (!Directory.Exists(currentDir))
{
Directory.CreateDirectory(currentDir);
}
StorageCreationPropertiesBuilder storageProperties = new("AppTokenCache", currentDir);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
storageProperties.WithLinuxUnprotectedFile();
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
storageProperties.WithMacKeyChain(_keyChainServiceName, _keyChainAccountName);
}
MsalCacheHelper tokenCacheHelper = await MsalCacheHelper.CreateAsync(storageProperties.Build());
tokenCacheHelper.RegisterCache(_confidentialClientApplication.AppTokenCache);
}
AuthenticationResult authResponse;
authResponse = await _confidentialClientApplication.AcquireTokenForClient(_scopes).ExecuteAsync(ct);
return authResponse;
}
/// <summary>
/// Handles sending the request and adding the token to the request.
/// </summary>
/// <param name="request">Request to be sent</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Headers.Authorization is null)
{
AuthenticationResult authResponse = await AuthenticateAsync(cancellationToken);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.AccessToken);
}
return await base.SendAsync(request, cancellationToken);
}
}
}