在我的SignalR Authentication原型中,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 configuration和SignalR Bearer Authentication
请求令牌的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();