1
- using Azure . Core ;
2
- using Azure . Identity ;
3
- using Microsoft . Extensions . Options ;
4
- using Microsoft . Graph . Beta ;
1
+ using Microsoft . Extensions . Options ;
5
2
using Microsoft . Identity . Client ;
6
- using Microsoft . Kiota . Abstractions . Authentication ;
7
3
using Plumsail . DataSource . Dynamics365 . BusinessCentral . Settings ;
8
- using Plumsail . DataSource . Dynamics365 . CRM ;
9
- using System ;
10
- using System . Net . Http ;
11
4
using System . Net . Http . Headers ;
12
- using System . Threading ;
13
- using System . Threading . Tasks ;
14
5
15
6
namespace Plumsail . DataSource . Dynamics365 . BusinessCentral
16
7
{
17
- public class HttpClientProvider
8
+ public class HttpClientProvider ( IOptions < AppSettings > settings )
18
9
{
19
- private readonly AzureApp _azureAppSettings ;
20
-
21
- public HttpClientProvider ( IOptions < AppSettings > settings )
22
- {
23
- _azureAppSettings = settings . Value . AzureApp ;
24
- }
10
+ private readonly AzureApp _azureAppSettings = settings . Value . AzureApp ;
25
11
26
12
public HttpClient Create ( )
27
13
{
@@ -38,27 +24,21 @@ public HttpClient Create()
38
24
}
39
25
}
40
26
41
- class OAuthMessageHandler : DelegatingHandler
27
+ class OAuthMessageHandler ( AzureApp azureAppSettings , HttpMessageHandler ? innerHandler = null )
28
+ : DelegatingHandler ( innerHandler ?? new HttpClientHandler ( ) )
42
29
{
43
- private readonly AzureApp _azureAppSettings ;
44
-
45
- public OAuthMessageHandler ( AzureApp azureAppSettings , HttpMessageHandler ? innerHandler = null ) : base ( innerHandler ?? new HttpClientHandler ( ) )
46
- {
47
- _azureAppSettings = azureAppSettings ;
48
- }
49
-
50
30
protected override async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
51
31
{
52
- var app = ConfidentialClientApplicationBuilder . Create ( _azureAppSettings . ClientId )
53
- . WithClientSecret ( _azureAppSettings . ClientSecret )
54
- . WithTenantId ( _azureAppSettings . Tenant )
32
+ var app = ConfidentialClientApplicationBuilder . Create ( azureAppSettings . ClientId )
33
+ . WithClientSecret ( azureAppSettings . ClientSecret )
34
+ . WithTenantId ( azureAppSettings . Tenant )
55
35
. Build ( ) ;
56
36
57
37
var cache = new TokenCacheHelper ( AzureApp . CacheFileDir ) ;
58
38
cache . EnableSerialization ( app . UserTokenCache ) ;
59
39
60
40
var account = await app . GetAccountAsync ( cache . GetAccountIdentifier ( ) ) ;
61
- var result = await app . AcquireTokenSilent ( [ "https://api.businesscentral.dynamics.com/.default" ] , account ) . ExecuteAsync ( ) ;
41
+ var result = await app . AcquireTokenSilent ( [ "https://api.businesscentral.dynamics.com/.default" ] , account ) . ExecuteAsync ( cancellationToken ) ;
62
42
request . Headers . Authorization = new AuthenticationHeaderValue ( "Bearer" , result . AccessToken ) ;
63
43
return await base . SendAsync ( request , cancellationToken ) ;
64
44
}
0 commit comments