Код: Выделить всё
public static void MapClientEndpoints (this IEndpointRouteBuilder routes)
{
var group = routes.MapGroup("/api/Client").WithTags(nameof(Client));
group.MapGet("/", async (HypnoContext db) =>
{
return await db.Client.ToListAsync();
})
.RequireAuthorization()
.WithName("GetAllClients")
.WithOpenApi();
}
Код: Выделить всё
builder.Services.AddScoped();
builder.Services.AddHttpClient("ServerAPI", client => {
client.BaseAddress = new Uri(builder.Configuration["FrontendUrl"]);
})
.AddHttpMessageHandler();
builder.Services.AddTransient(sp =>
sp.GetRequiredService().CreateClient("ServerAPI"));
Код: Выделить всё
public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
{
public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
NavigationManager navigationManager)
: base(provider, navigationManager)
{
ConfigureHandler(
authorizedUrls: new[] { "https://localhost:7241" });
}
}
Код: Выделить всё
@inject HttpClient httpClient
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Get, "/api/Client");
await httpClient.SendAsync(request);
System.AggregateException: «Некоторые службы не может быть
создан (Ошибка при проверке дескриптора службы
'ServiceType: MyApp.CustomAuthorizationMessageHandler
Lifetime: Scoped ImplementationType:
MyApp.CustomAuthorizationMessageHandler': невозможно разрешить
службу для типа
'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider'
при попытке активировать
'MyApp.CustomAuthorizationMessageHandler '.)'

Итак, я попробовал добавьте следующий код
Код: Выделить всё
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtIssuer"],
ValidAudience = builder.Configuration["JwtAudience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
builder.Configuration["JwtSecurityKey"]))
};
});
Подробнее здесь: https://stackoverflow.com/questions/792 ... l-identity