简体   繁体   English

在从 Angular 对 SignalR 集线器的调用中包含身份验证标头(带有访问令牌)

[英]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.

相关问题 来自角度4的Owin令牌调用请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Owin token call from angular 4 No 'Access-Control-Allow-Origin' header is present on the requested resource 有什么方法可以直接从 Angular 连接 Azure SignalR 集线器? - Is there any way to connect Azure SignalR Hub directly from Angular? Angular 7 连接到 SignalR Hub 失败 - Angular 7 connection to SignalR Hub failed 使用Angular 5(隐式身份验证)从Instagram重定向中提取ACCESS_TOKEN - Extracting ACCESS_TOKEN from Instagram redirect with Angular 5(Implicit Authentication) Angular 6中的强类型SignalR集线器 - Strongly-Typed SignalR Hub in Angular 6 在 Angular 中创建多个 Signalr 集线器代理 - Creating multiple Signalr hub proxies in Angular 角度2,信号器断开后自动重新连接至集线器 - angular 2 with signalr auto reconnecting to hub when disconnected SignalR:无法访问默认的集线器方法 - SignalR : Cannot access default hub methods 使用Azure AD身份验证库(角度5中的ADAL)获取Id_token,但是如何从Id_token获取访问令牌? - getting Id_token using Azure AD Authentication Library(ADAL in angular 5), but how to get access token from Id_token? 从 Controller 调用 SignalR 核心集线器方法时,angular 客户端未接收数据 - When calling SignalR Core Hub method from Controller the angular client is not receiving data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM