How can I authenticate a websocket connection where client and server reside on seperate domains?

淺唱寂寞╮ 提交于 2019-12-23 15:02:17

问题


I'm currently playing around with SignalR and websockets. From my research, it seems, as websockets do not support custom headers, there's basically only two ways to authenticate a websocket connection during token based authentication.

1) Passing the token in the query string 2) Storing the token in a cookie which then gets passed to the server when WithCredentials is set to true

The first method isn't great practice - even through websocket communication is encrypted, query strings may be logged by servers etc.

The second method I have got working on my local machine but it doesn't work once deployed because my client and server reside on different domains. So basically, I have an Angular site that has one domain (eg. client.com) and a WebAPI site that alls CORS with a completely different domain (eg. server.com). On my browser, if I'm on client.com, I cannot set a cookie that gets sent to server.com on a request.

What is a good way to authenticate websockets when client and server sit on different domains?


回答1:


The WebSocket Protocol specification doesn't specify any particular way for authentication. You need to perform the authentication during the handshake phase and for that you can use any HTTP authentication mechanism like Basic, Digest, etc.

Further you could look into JWT token based authentication. Angular app can store the token in local storage and send it as a Transport header during the handshake request to the server. If the token is invalid, server can terminate the WebSocket connection upgrade request and the Angular app can re-direct the user to login page.



来源:https://stackoverflow.com/questions/47495817/how-can-i-authenticate-a-websocket-connection-where-client-and-server-reside-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!