出现这个问题时,一般是因为缺少适当的配置。可以在 appsettings.json 中添加以下代码来配置 IdentityServer:
"IdentityServer": {
"Clients": {
"BlazorWasm": {
"Profile": "IdentityServerSPA"
}
},
"DefaultProfile": "IdentityServerSPA",
"Profile": {
"IdentityServerSPA": {
"IdentityClaims": [
"name",
"email",
"role"
]
}
}
}
还需要在 Program.cs 文件中对身份验证进行配置:
builder.Services.AddOidcAuthentication(options =>
{
options.ProviderOptions.Authority = "https://localhost:5005";
options.ProviderOptions.ClientId = "BlazorWasm";
options.ProviderOptions.ResponseType = "code";
});
如果还是出现错误,可以检查一下 identity.js 文件是否已包含在项目中。