SignalR 与 JS 客户端上的 accessTokenFactory 的连接无法与 connectionId 和 UserIdentifier 连接,但 .Net 客户端没问题

Dol*_*ead 0 c# jwt signalr asp.net-core

在我的 SignalR 身份验证原型中,JS 客户端没有 Context.UserIdentifier,当连接到 JS 客户端的 SignalR 集线器时,connectionId 将为 0。它与 .NET 客户端配合良好。

想知道构建连接是否错误?这是我的连接:

state.connection = new signalR.HubConnectionBuilder()
                .withUrl("/chatHub", {
                    accessTokenFactory: async () => {
                        axios.post('https://localhost:44384/api/account/token', { email: email, password: password })
                            .then(async (response) => {
                                if (typeof response === 'undefined') {
                                    return;
                                }
                                else {
                                    console.log(response.data);
                                    console.log(state.connection.id);
                                    return response.data;
                                }
                            })
                            .catch((error) => {
                                console.log(error);
                            });
                    }
                })
                .build();   
Run Code Online (Sandbox Code Playgroud)

WPF 客户端使用以下代码可以很好地连接令牌:

_connection = new HubConnectionBuilder()
            .WithUrl("https://localhost:44384/ChatHub", options =>
            {
                options.AccessTokenProvider = async () =>
                {
                    var tokenUserCommand = new TokenUserCommand
                    {
                        Email = emailTextBox.Text,
                        Password = passwordBox.Password
                    };

                    var token = await RestService.PostAsync<string>("account/token", tokenUserCommand);
                    return token;
                };

            })
            .Build();
Run Code Online (Sandbox Code Playgroud)

我在哪里找到SignalR 身份验证配置SignalR 承载身份验证

这是我的原型项目的来源

Dol*_*ead 7

请求 token 的 JS 客户端只需要在 axios.post 之前添加一个await关键字即可。这种菜鸟错误花费了比应有的时间更多的时间。

state.connection = new signalR.HubConnectionBuilder()
            .withUrl("https://localhost:44384/ChatHub", {
                accessTokenFactory: async () => {
                    if (state.accessToken === null) {
                        await axios.post('https://localhost:44384/api/account/token', { email: email, password: password })
                            .then(async (response) => {
                                if (typeof response === 'undefined') {
                                    return;
                                }
                                else {
                                    state.accessToken = response.data;
                                }
                            })
                            .catch((error) => {
                                console.log(error);
                            });
                    }
                    return state.accessToken;
                }
            })
            .build();
Run Code Online (Sandbox Code Playgroud)