【问题标题】:SignalR connection with accessTokenFactory on JS client doesn't connect with connectionId and UserIdentifier but .Net client is fineSignalR 与 JS 客户端上的 accessTokenFactory 的连接不与 connectionId 和 UserIdentifier 连接,但 .Net 客户端很好
【发布时间】:2019-04-24 21:51:51
【问题描述】:

在我的 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();   

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();

我在哪里找到SignalR Authentication configurationSignalR Bearer Authentication

这里是source for my prototype project

【问题讨论】:

    标签: c# asp.net-core jwt signalr


    【解决方案1】:

    请求令牌的 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多