如何将自定义标头添加到SignalR的Typescript客户端? [英] How do I add custom headers to SignalR's Typescript client?
问题描述
我正在尝试在标头中传递承载令牌以授权客户端连接.SignalR集线器通过从标头中获取承载令牌来授权客户端.我无法修改SingalR集线器代码以使用查询字符串来获取令牌,也不能使用内置的JWT令牌功能.
I am trying to pass a bearer token in the header to authorize client connections. The SignalR hub authorizes client by grabbing the bearer token from the header. I cannot modify the SingalR hub code to use a query string to get the token, nor can I use the built-in JWT token functionality.
如何向SignalR Typescript客户端添加自定义标头?
How do I add a custom header to the SignalR typescript client?
我唯一想做的就是重写HttpClient以添加标头,但是我不确定这是否可行或如何执行.
The only thing I can think to do is override the HttpClient in to add the header however I am not sure if this is possible or how to do this.
我正在Angular 8中使用@ microsoft/signalr和最新的@ microsoft/signalr软件包.
I am using @microsoft/signalr in Angular 8 and the latest @microsoft/signalr package.
推荐答案
在建立连接时,可以在选项中添加自定义HttpClient.
You can add a custom HttpClient in the options when building the connection.
它看起来像这样:
class CustomHttpClient extends HttpClient {
public send(request: HttpRequest): Promise<HttpResponse> {
request.headers = { ...request.headers, "customHeader": "value" };
// ... http call
}
}
let hubConnection = new HubConnectionBuilder()
.withUrl(url, { httpClient: new CustomHttpClient() })
.build();
可能可以从 DefaultHttpClient
继承,并在您的send中调用DefaultHttpClient的send,因此您无需自己实现Http调用.
It might be possible to inherit from the DefaultHttpClient
and in your send you call the DefaultHttpClient's send so you don't need to implement the Http calls yourself.
https://github.com/dotnet/aspnetcore/issues正在跟踪一流的支持/14588 .
这篇关于如何将自定义标头添加到SignalR的Typescript客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!