forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaderDelegatingHandler.cs
More file actions
35 lines (31 loc) · 1.13 KB
/
HeaderDelegatingHandler.cs
File metadata and controls
35 lines (31 loc) · 1.13 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 Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ApiGateway.Helper
{
public class HeaderDelegatingHandler : DelegatingHandler
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HeaderDelegatingHandler(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
IEnumerable<string> headerValues;
if (request.Headers.TryGetValues("AccessToken", out headerValues))
{
string accessToken = headerValues.First();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Remove("AccessToken");
}
return await base.SendAsync(request, cancellationToken);
}
}
}