mirror of
https://github.com/CannedNet/CannedNet.git
synced 2026-10-25 23:18:54 -07:00
25 lines
850 B
C#
25 lines
850 B
C#
using System.Net.Http.Headers;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
namespace CannedNet.Admin.Services;
|
|
|
|
public class JwtBearerHandler : DelegatingHandler
|
|
{
|
|
private readonly AuthenticationStateProvider _authStateProvider;
|
|
|
|
public JwtBearerHandler(AuthenticationStateProvider authStateProvider)
|
|
{
|
|
_authStateProvider = authStateProvider;
|
|
}
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
var state = await _authStateProvider.GetAuthenticationStateAsync();
|
|
var token = state.User.FindFirst("access_token")?.Value;
|
|
if (token != null)
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
return await base.SendAsync(request, cancellationToken);
|
|
}
|
|
}
|