-
Notifications
You must be signed in to change notification settings - Fork 218
/
TokenAcquisitionCredentialProvider.cs
42 lines (38 loc) · 1.46 KB
/
TokenAcquisitionCredentialProvider.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Graph;
namespace Microsoft.Identity.Web
{
/// <summary>
/// Authentication provider based on ITokenAcquisition.
/// </summary>
internal class TokenAcquisitionCredentialProvider : IAuthenticationProvider
{
public TokenAcquisitionCredentialProvider(ITokenAcquisition tokenAcquisition, IEnumerable<string> initialScopes)
{
_tokenAcquisition = tokenAcquisition;
_initialScopes = initialScopes;
}
private ITokenAcquisition _tokenAcquisition;
private IEnumerable<string> _initialScopes;
/// <summary>
/// Adds a bearer header to an HttpRequestMessage.
/// </summary>
/// <param name="request">HttpRequest message to authenticate.</param>
/// <returns>A Task (as this is an async method).</returns>
public async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
request.Headers.Add(
Constants.Authorization,
string.Format(
CultureInfo.InvariantCulture,
"{0} {1}",
Constants.Bearer,
await _tokenAcquisition.GetAccessTokenForUserAsync(_initialScopes).ConfigureAwait(false)));
}
}
}