Created
May 14, 2021 06:03
-
-
Save Platonenkov/b117374f707ffaecd7c080081c7922e2 to your computer and use it in GitHub Desktop.
Get token from context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authentication; | |
using Microsoft.AspNetCore.Http; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
namespace Management.Ui.Services | |
{ | |
public class TokenContainer | |
{ | |
private readonly IHttpContextAccessor _httpContextAccessor; | |
public TokenContainer(IHttpContextAccessor httpContextAccessor) | |
{ | |
_httpContextAccessor = httpContextAccessor; | |
} | |
protected async Task AddRequestHeaders(HttpClient httpClient) | |
{ | |
var accessToken = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token"); | |
httpClient.DefaultRequestHeaders.Accept.Add( | |
new MediaTypeWithQualityHeaderValue("application/json")); | |
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment