How to add cookies

Visto 123 veces
Saltar al primer mensaje no leído

Markus Blaser

no leída,
27 oct 2021, 12:56:2327/10/21
a WebSocket++
Is it possible to add cookies to a connection ?

Peter Thorson

no leída,
28 oct 2021, 8:34:0128/10/21
a Markus Blaser,WebSocket++
Hi Markus,

Yes, you can set and read cookies by adding/accessing the appropriately named and formatted HTTP headers.

Details:
Under the hood Cookies are an HTTP header set by either the client or the server on the request or response. The names and syntax for the Header and the associated values are described at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie (for client to server) and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie (for server to client).

WebSocket++ allows clients to set arbitrary HTTP headers on outgoing connections via connection::replace_header, connection::append_header, and connection::remove_header. Servers can set server to client cookies by using the same functions in the validate handler (note that the header name for server-to-client is "Set-Cookie" rather than just "Cookie").

Cookies can be read by using the connection::get_request_header function. For servers this is available in the validate and later handlers. In the validate handler cookies can be read and used to decide whether to accept or reject the connection. In both servers and clients this function is also available in the open handler for reading to perform post-connection authentication, session management, etc

Best,

Peter

On 2021-10-27, at 11:56, Markus Blaser <markus...@bluewin.ch> wrote:

Is it possible to add cookies to a connection ?

--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/websocketpp/0b275bc7-c777-4eb6-aa0e-39392d3b9b42n%40googlegroups.com.

Markus Blaser

no leída,
31 oct 2021, 9:54:2231/10/21
a WebSocket++
Hi Peter,
Thank you for the fast response. I am working on a websocket client on a Jetson Nano that connects to a AspNet Core  5.0 server. Adding the line  con-> append_header("Cookie", "id=12345678"); does the job and the cookie with key id and value 12345678 appears in the CookieCollection of the incoming request (AspNet Core HttpRequest class).
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos