首页 > 解决方案 > javascript SignalR 客户端和 .net 核心后端之间的不稳定连接

问题描述

我的 JavaScript SignalR 客户端经常连接到我的 IdentityServer4 的 asp.net 核心实现。

在客户端我看到:

Information: WebSocket connected to wss://localhost:5001/signal/satellite?
idsrv.session=4ec93d2f4c3b9a970ff82a537ae04d97&id=0eUBg2-xobp4CNttuIQJcg

但是,它在很多时候也没有连接,错误是 JSON 中的位置 0 存在意外标记。

似乎它运行得很好,然后又很糟糕,即使我重新登录,它仍然会抛出错误。在浏览器中,调用总是或多或少类似于:

https://localhost:5001/signal/satellite?
idsrv.session=89364018a975e4fefff9ad0869b1ae09

我似乎不需要中间件来将查询字符串值移动到标题中,我仍然在 onConnect 期间让用户在我的集线器中使用他们的子声明等。但是,如果我确实在 app.UseAuthentication 之前放入中间件() 并注意:

context.Request.QueryString.Value

当它与服务器成功握手时,我看到:

idsrv.session=89364018a975e4fefff9ad0869b1ae09

但是如果没有,我会看到:

?client_id=internal&redirect_uri=https%3A%2F%2Flocalhost%3A5001%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20profile%20offline_access%20Harvey&response_mode=form_post&nonce=636817245670169226.ZWM2OWQ0ZWEtOTQzMC00YTJlLWI4MzQtYmIxZDZjOWVlYjg5ZTA4NTU2M2QtNjczZi00MTlmLThjYmQtZWUzZTQ1ODMzNDQ0&state=CfDJ8MCxOjJiJLdKqZrnPwOHDhqMnzWz6MqRb03SxToClqQ1F3n9g8yLdW683HRpZSHd-5wkN-6je4tHJkA8sc5i6YoKRxtMHwnWqxVW5-nXFaaH0TfOLUeqfxDzXLxnftmWFXLjK3Y7b6R2WzcDLEjChU1_Fk6X64SAHNRqeizGDPzRhxpV0U5w19Bbt7pUyRbYymn2WNedCS1F7g_wtwtJXDjCzWKBxqvPZ5Dtg99gxKkANalKYs7C4-fm7YdD0gFvsuV4CXsu0T06MjzID_zpA_F7TmSue4vGI-0_qY55Swc5mbLWUwKHtj6ZTfOG4UmTEP_hbj2PO9w2oNg9TWqTPtDC3-qSl1fTUkY0EtCwbA7F&x -client-SKU=ID_NETSTANDARD1_4&x-client-ver=5.2.0.0

所以我要么不需要这样做,因为它自己成功,要么查询字符串永远不会进入中间件移动(这可能是它不能自己做的原因)

我缺少什么让它始终如一地工作?

将混合客户端添加到 IdentityServer4:

        new Client
        {
            ClientId = "mvc",
            ClientName = "MVC Client",
            AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

            ClientSecrets = 
            {
                new Secret("secret".Sha256())
            },

            RedirectUris = { "http://localhost:5002/signin-oidc" },
            PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

            AllowedScopes = 
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                "api1"
            },
            AllowOfflineAccess = true
        }

然后在 mvc 客户端上启动:

 ServiceCollection.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";

                options.Authority = "https://localhost:5001";
                options.RequireHttpsMetadata = false;
                options.ClientSecret = "secret";
                options.ClientId = "mvc";

                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;

                options.Scope.Add("offline_access");
                options.Scope.Add("api1");
                options.ClaimActions.MapJsonKey("website", "website"); 
            });

这是根据示例建模的:Quickstart5_HybridAndApi

在服务器上的 SignalR 中:

        [Authorize]
        public class SignalRSignalHub : Hub
        {
            public override Task OnConnectedAsync()
            {
                var context = Context.GetHttpContext();
                return base.OnConnectedAsync();
            }
        }

SignalR JavaScript 客户端:

    self.signalRConnection = new signalR.HubConnectionBuilder()
.withUrl("/signal/satellite?" + document.cookie).build();

标签: identityserver4asp.net-core-signalrsignalr-2

解决方案


推荐阅读