在Blazor应用程序中启用身份验证时,可能会出现身份验证仍带有false的问题。这通常是因为身份验证的资源未正确添加到应用程序的Startup.cs文件中。
要解决此问题,请按照以下步骤操作:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/login"; });
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
public override Task
var user = new ClaimsPrincipal(identity);
return Task.FromResult(new AuthenticationState(user));
}
}
static async Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped();
builder.RootComponents.Add("app");
await builder.Build().RunAsync();
}