-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenHandler.cs
More file actions
35 lines (32 loc) · 1.08 KB
/
Copy pathTokenHandler.cs
File metadata and controls
35 lines (32 loc) · 1.08 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
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System;
using System.Diagnostics;
namespace EBScan
{
public class TokenHandler : DelegatingHandler
{
private readonly TokenService tokenService;
public TokenHandler(TokenService tokenService) : base(new HttpClientHandler())
{
this.tokenService = tokenService;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
string token = await tokenService.GetTokenAsync();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
try
{
return await base.SendAsync(request, cancellationToken);
}
catch (Exception ex)
{
Debug.WriteLine($"TokenHandler: Exception {ex.Message}");
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
}
}