要在Blazor Server应用程序中使用Windows身份验证进行SignalR,您需要进行以下设置:
Startup.cs
文件中,确保在ConfigureServices
方法中添加以下代码:services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
Configure
方法中添加以下代码:app.UseAuthentication();
/hubs
路径下。在Startup.cs
中的ConfigureServices
方法中添加以下代码:services.AddSignalR().AddHubOptions(options =>
{
options.EnableDetailedErrors = true;
options.ClientTimeoutInterval = TimeSpan.FromSeconds(30);
}).AddNegotiate();
[Authorize]
特性,以确保只有经过身份验证的用户可以访问它。例如:[Authorize]
public class YourHubClass : Hub
{
// your hub code here
}
withCredentials: true
。例如:const connection = new signalR.HubConnectionBuilder()
.withUrl("/hubs/yourHub", { withCredentials: true })
.build();
这些步骤将启用Windows身份验证并确保SignalR Hub只能由经过身份验证的用户访问。如果仍然收到401错误,请确保您的应用程序已配置为使用Windows身份验证,并且用户具有适当的权限。
上一篇:Blazor Server: 使用身份验证进行 Web API 调用
下一篇:Blazor Server端 - 如何使用RevalidatingServerAuthenticationStateProvider来持续检查令牌过期?