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 承载身份验证
请求 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)
| 归档时间: |
|
| 查看次数: |
7032 次 |
| 最近记录: |