Passing token through http Headers SignalR

前端 未结 6 1047
陌清茗
陌清茗 2020-12-04 16:45

I can see that there is an option in HubConnection to pass parameters through url request from client. Is there any way to pass specific token through http headers from JS o

6条回答
  •  天涯浪人
    2020-12-04 17:26

    You can add headers on either Connection or HubConnection using the .NET client (as @abnanda mentions):

    var connection = new Connection("http://signalr/");
    connection.Headers.Add("custom-header", "value");
    

    On the server side you can retrieve the header value from IRequest in e.g. OnConnected:

    var header = request.Headers["custom-header"];
    

    However, I have not yet found a way to do this with the JS client (there is no headers or addHeaders on the $connection object).

    I have posted a question on that at: http://forums.asp.net/t/1910020.aspx/1?Add+custom+HTTP+header+on+client+side+connect+call+SignalR+1+1+1+

    EDIT: The headers are not exposed since the websocket client (in browser) does not support it (so it would not be possible to implement it across transports). See David Fowlers response in the post above.

提交回复
热议问题