如何在 SignalR 中为某些但不是全部集线器设置集成 Windows 身份验证

问题描述 投票:0回答:2

我需要对集线器使用 Windows 身份验证,但由于我在更大的系统中工作,我只能通过 GlobalHost 访问 SignalR 管道。

我尝试过使用

AuthorizedAttribute
,但这并没有设置身份,因为没有注册授权模块。

我尝试过设置

GlobalHost.HubPipeline.RequireAuthentication();

设置了身份,但由于它设置了全局授权,其他集线器无法连接。

最后,我尝试添加一个自定义授权模块,该模块应该仅适用于我想要的一个中心。 但是,一旦我对一个集线器进行例外处理,身份就不会再次设置,并且我会从授权集线器收到未经授权的响应。

c# signalr windows-authentication
2个回答
0
投票

我通常将身份验证属性放在我的集线器类之上。

 [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
 public class myChat : Hub 
 {
 }

这是基于令牌的身份验证,您需要配置从客户端发送身份验证令牌。 https://learn.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-3.1


0
投票

我的问题是我的 OWIN 启动不包括允许对已设置的 HttpListener 进行 Windows 身份验证(我的集线器是在 Windows 服务中设置的,而不是 IIS)。下面的

AuthenticationSchemes
行是实现此功能的关键。

    public void Configuration(IAppBuilder app)
    {
        if (app.Properties.TryGetValue(typeof(HttpListener).FullName, out object listener))
        {
            ((HttpListener)listener).AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
        }
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR("/sfm", new HubConfiguration
        {
            EnableDetailedErrors = true,
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.