forked from man-group/dapr-sidekick-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectInvocationHandler.cs
42 lines (38 loc) · 1.25 KB
/
DirectInvocationHandler.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
#if !NET35
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Man.Dapr.Sidekick.Security;
namespace Man.Dapr.Sidekick.DaprClient
{
internal class DirectInvocationHandler : DelegatingHandler
{
private readonly IDaprApiTokenAccessor _tokenAccessor;
public DirectInvocationHandler(IDaprApiTokenAccessor tokenAccessor)
{
_tokenAccessor = tokenAccessor;
}
/// <inheritdoc />
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var apiTokenHeader = DaprClient.GetDaprApiTokenHeader(_tokenAccessor.DaprApiToken);
var isValidToken = apiTokenHeader.Key != null && apiTokenHeader.Value != null;
try
{
if (isValidToken)
{
request.Headers.Add(apiTokenHeader.Key, apiTokenHeader.Value);
}
return await base.SendAsync(request, cancellationToken);
}
finally
{
if (isValidToken)
{
request.Headers.Remove(apiTokenHeader.Key);
}
}
}
}
}
#endif