[英]Including authentication header (with access token) in a call to a SignalR hub from Angular
On the back end, I have an ASP.Net Core 2.2 Web API.在后端,我有一个 ASP.Net Core 2.2 Web API。 In it I have a SignalR Core hub.
在其中我有一个 SignalR Core 集线器。 On the front we have an Angular 5 application.
在前面我们有一个 Angular 5 应用程序。 We are using Auth0 as our authentication server.
我们使用 Auth0 作为我们的身份验证服务器。
When the front-end calls any API endpoints, it sends along with the query an authentication header which contains an access token.当前端调用任何 API 端点时,它会随查询一起发送一个包含访问令牌的身份验证标头。 As all the API endpoints have the [Authorize] decorator, this is required.
由于所有 API 端点都有 [Authorize] 装饰器,因此这是必需的。
The front-end developer is now working on a chat system for the client app, using SignalR, but he doesn't know how to do the same with the calls to the SignalR hub.前端开发人员现在正在使用 SignalR 为客户端应用程序开发聊天系统,但他不知道如何对 SignalR 集线器的调用执行相同的操作。 Ie to include the authentication header (with an access token) when he invokes the SignalR hub methods in Angular.
即在他调用 Angular 中的 SignalR 集线器方法时包含身份验证标头(带有访问令牌)。
My question is, is it possible to do this?我的问题是,有可能做到这一点吗? I understand SignalR uses Web Sockets when possible.
我了解 SignalR 尽可能使用 Web 套接字。 So is it possible to include an authorization header with a web socket, in the same way one does with regular HTTP calls to the API?
那么是否可以在 Web 套接字中包含授权标头,就像对 API 的常规 HTTP 调用一样? If it's possible, a sample would be very much appreciated!
如果可能,将非常感谢您提供样品!
Currently he has a simple test code setup in Angular:目前他在 Angular 中有一个简单的测试代码设置:
this.hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('));
this.hubConnection.on('ReceiveMessage', (userId: string, message: string) => {
const text = `${userId}: ${message}`;
this.messages.push(text);
});
this.hubConnection
.invoke('SendMessage', 'john', 'hello world')
.catch(err => console.error(err));
} }
将传输类型更改为长轮询,访问令牌将添加到 HTTP 标头上
// Connect, using the token we got.
this.connection = new signalR.HubConnectionBuilder()
.withUrl("/hubs/chat", { accessTokenFactory: () => this.loginToken })
.build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.