在Blazor应用程序中,可以使用Windows身份验证来验证用户的身份。但是,有时候在使用Kestrel服务器时,可能会遇到仅在本地工作的问题,或者在身份验证握手请求之间收到匿名请求的问题。
以下是一个可能的解决方案,其中包含代码示例:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseAuthentication();
app.UseAuthorization();
// ...
}
AuthorizeView
组件来限制只有经过身份验证的用户才能访问某些内容:
Welcome, authenticated user!
Please log in to access this content.
launchSettings.json
文件中添加"windowsAuthentication": true
配置项:{
// ...
"profiles": {
"IIS Express": {
// ...
"windowsAuthentication": true
},
"BlazorApp": {
// ...
"windowsAuthentication": true
}
}
}
这样配置之后,应该能够在Kestrel服务器上进行Windows身份验证并正常工作,而不仅仅是在本地环境中。
希望这个解决方案能够帮助您解决问题!