Код: Выделить всё
using LogisticsTruckScales.BlazorServer.Components;
using LogisticsTruckScales.BlazorServer.Services.Secuirty;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.Circuits;
using Microsoft.Identity.Web;
using MudBlazor.Services;
using System.Security.Claims;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMudServices();
builder.Services.AddScoped();
builder.Services.AddScoped();
// Add services to the container
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddScoped();
builder.Services.AddHttpClient("ScaleTransactionManager", client =>
{
client.BaseAddress = new Uri(builder.Configuration["ScaleTransactionManager:BaseUrl"]);
}).AddHttpMessageHandler();
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapRazorComponents().AddInteractiveServerRenderMode();
app.Run();
Код: Выделить всё
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web;
using System.Net.Http.Headers;
using System.Security.Claims;
using static System.Formats.Asn1.AsnWriter;
namespace LogisticsTruckScales.BlazorServer.Services.Secuirty
{
public class ScaleTransactionManagerAuthorizationHandler : DelegatingHandler
{
private readonly CircuitServicesAccessor circuitServicesAccessor;
private readonly IConfiguration _configuration;
public ScaleTransactionManagerAuthorizationHandler(CircuitServicesAccessor circuitServicesAccessor, IConfiguration configuration)
{
this.circuitServicesAccessor = circuitServicesAccessor;
_configuration = configuration;
}
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var authStateProvider = circuitServicesAccessor.Services
.GetRequiredService();
var authState = await authStateProvider.GetAuthenticationStateAsync();
//I have the auth state here. I want to pass the access token to the request.
// Attach the token to the request
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return await base.SendAsync(request, cancellationToken);
}
}
}
Код: Выделить всё
using Microsoft.AspNetCore.Components.Server.Circuits;
namespace LogisticsTruckScales.BlazorServer.Services.Secuirty
{
public class CircuitServicesAccessor
{
static readonly AsyncLocal blazorServices = new();
public IServiceProvider? Services
{
get => blazorServices.Value;
set => blazorServices.Value = value;
}
}
public class ServicesAccessorCircuitHandler(
IServiceProvider services, CircuitServicesAccessor servicesAccessor)
: CircuitHandler
{
public override Func CreateInboundActivityHandler(
Func next) =>
async context =>
{
servicesAccessor.Services = services;
await next(context);
servicesAccessor.Services = null;
};
}
public static class CircuitServicesServiceCollectionExtensions
{
public static IServiceCollection AddCircuitServicesAccessor(
this IServiceCollection services)
{
services.AddScoped();
services.AddScoped();
return services;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... se-in-ihtt