50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
using Atomx.Admin.Client.Services;
|
||
using Atomx.Admin.Client.Utils;
|
||
using Blazored.LocalStorage;
|
||
using Microsoft.AspNetCore.Components.Authorization;
|
||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||
|
||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||
|
||
// 注册本地存储(WASM 使用 localStorage 保存 tokens)
|
||
builder.Services.AddBlazoredLocalStorageAsSingleton();
|
||
|
||
// 授权/身份(WASM 使用轻量的 AuthenticationStateProvider)
|
||
builder.Services.AddAuthorizationCore();
|
||
builder.Services.AddCascadingAuthenticationState();
|
||
builder.Services.AddSingleton<AuthenticationStateProvider, PersistentAuthenticationStateProvider>();
|
||
|
||
// 权限 & 本地化(客户端实现)
|
||
builder.Services.AddScoped<IPermissionService, ClientPermissionService>();
|
||
builder.Services.AddScoped<IconsExtension>();
|
||
builder.Services.AddScoped<ILocalizationService, LocalizationClientService>();
|
||
|
||
// 注册用于自动附带 token & 刷新的 DelegatingHandler
|
||
builder.Services.AddScoped<AuthHeaderHandler>();
|
||
|
||
// 命名 HttpClient(应用内统一使用 ApiClient),并将 AuthHeaderHandler 加入请求管道
|
||
var apiBase = builder.Configuration["WebApi:ServerUrl"] ?? builder.HostEnvironment.BaseAddress;
|
||
builder.Services.AddHttpClient("ApiClient", client =>
|
||
{
|
||
client.BaseAddress = new Uri(apiBase);
|
||
})
|
||
.AddHttpMessageHandler<AuthHeaderHandler>();
|
||
|
||
// 注册一个不带 AuthHeaderHandler 的 HttpClient,用于刷新 token(避免循环调用)
|
||
// 该 client 名称在 AuthHeaderHandler 中使用 "RefreshClient"
|
||
builder.Services.AddHttpClient("RefreshClient", client =>
|
||
{
|
||
client.BaseAddress = new Uri(apiBase);
|
||
});
|
||
|
||
// 默认 HttpClient(方便注入 HttpClient)
|
||
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ApiClient"));
|
||
|
||
// 在 WASM DI 中注册 HttpService,使用上面注入的 HttpClient 实例
|
||
builder.Services.AddScoped<HttpService>(sp => new HttpService(sp.GetRequiredService<HttpClient>()));
|
||
|
||
builder.Services.AddAntDesign();
|
||
|
||
|
||
await builder.Build().RunAsync();
|