Skip to content

Instantly share code, notes, and snippets.

@Platonenkov
Created May 14, 2021 06:03
Show Gist options
  • Save Platonenkov/b117374f707ffaecd7c080081c7922e2 to your computer and use it in GitHub Desktop.
Save Platonenkov/b117374f707ffaecd7c080081c7922e2 to your computer and use it in GitHub Desktop.
Get token from context
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