使用IdentityServer承载的SignalR将不会从Hub接收任何JWTBearerEvents

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

我们有一个api(.net核心2.2),它使用IdentityServerAuthenticationDefaults.AuthenticationScheme为所有控制器工作正常。

我们现在决定为会议服务添加SignalR Hub。仅当我们删除authorize属性[Authorize(AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme)]时,集线器才能正常工作

我们尝试使用以下两种方法(TokenRetriever或JwrBearerEvents)处理查询中的令牌:

services.AddAuthentication()
        .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, options =>
        {
            options.Authority = AuthURL;
            options.SupportedTokens = SupportedTokens.Jwt;
            options.RequireHttpsMetadata = HttpsSetting;
            options.ApiName = APIs.API_Commerce;
            options.TokenRetriever = new Func<HttpRequest, string>(req =>
            {
                var fromHeader = TokenRetrieval.FromAuthorizationHeader();
                var fromQuery = TokenRetrieval.FromQueryString();
                return fromHeader(req) ?? fromQuery(req);
            });
            options.JwtBearerEvents.OnMessageReceived = context =>
                {
                    var accessToken = context.Request.Query["access_token"];

                    // If the request is for our hub...
                    var path = context.HttpContext.Request.Path;
                    if (!string.IsNullOrEmpty(accessToken) &&
                        (path.StartsWithSegments("/hubs/")))
                    {
                        // Read the token out of the query string
                        context.Token = accessToken;
                    }
                    return Task.CompletedTask;
                };
        });

出于某种原因,这些仅在我们调用控制器时触发,但忽略来自客户端的所有调用方法。

请注意,我们有一个AuthServer,它提供令牌和API。我们正在为客户端使用带有aspnet / signalr模块的角度7。

.net-core jwt signalr identityserver4
1个回答
0
投票

我发现了问题......

  1. app.UseAuthentication()已在Configure中添加
  2. 将默认方案添加到身份验证并删除onmessagereceived - > qazxsw poi

只需提及.net core 2.2你必须指定一个来源(withOrigins)并且不能使用Any ..

© www.soinside.com 2019 - 2024. All rights reserved.