【发布时间】:2018-06-15 14:10:48
【问题描述】:
我制作了一个使用 Bearer Token 进行身份验证的 asp.net core 2.0 SignalR Hub。现在我对如何通过 SignalR Angular 5 客户端连接它有点迷茫。如果我从 Hub 中删除授权,我实际上可以连接,因此连接正常,现在我相信我只需将 Authorization Bearer 添加到连接的 Http Headers 即可。
我的 Angular 5 项目的 package.json 文件中的 SignalR 客户端引用:"@aspnet/signalr-client": "^1.0.0-alpha2-final"
我的 Angular 组件:
import { Component, OnInit } from '@angular/core';
import { finalize } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
import { AuthenticationService } from '../core/authentication/authentication.service';
import { HubConnection } from '@aspnet/signalr-client';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
quote: string;
isLoading: boolean;
jwtToken:string;
private hubConnection: HubConnection;
constructor(
private _http: HttpClient,
private _auth : AuthenticationService,
private _toastr: ToastrService) { }
ngOnInit() {
this.isLoading = false;
this.jwtToken = this._auth.currentToken;
this.hubConnection = new HubConnection('http://localhost:27081/hub/notification/');
this.hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.error('Error while establishing connection :(', err));
this.hubConnection.on("send", data => {
console.log(data);
});
}
showToastr(){
this._toastr.success('Hello world!', 'Toastr fun!');
}
}
由于阅读了类似的问题,我尝试了:this.hubConnection.Headers.Add("token", tokenValue); 但它不起作用,Headers 属性不存在。
如何将 Bearer 令牌添加到 HubConnection 的 Http Headers?
感谢您的帮助
【问题讨论】:
标签: asp.net angular signalr.client