Joe Phillips
Joe Phillips

Reputation: 51200

How do I set a custom Sec-WebSocket-Protocol using a C# ClientWebSocket?

I am attempting to authorize my connection to a 3rd party websocket host and they require a JWT be set as the protocol.

I am able to include the header like this but I am getting an error back. Is there a better way to do this?

var client = new ClientWebSocket();
client.Options.SetRequestHeader("Sec-WebSocket-Protocol", "my JWT");
await client.ConnectAsync(...);

The error I'm getting is something like this:

The WebSocket client request requested '' protocol(s),
but server is only accepting '...my JWT...' protocol(s).

My attempt at making this work in C# is unsuccessful although I am able to connect properly via Postman Websockets (beta) by just setting the Sec-WebSocket-Protocol header and then Postman figures out the other headers automatically.

Upvotes: 2

Views: 2824

Answers (1)

Joe Phillips
Joe Phillips

Reputation: 51200

The solution that seems to have worked is that I am not allowed to set the Sec-WebSocket-Protocol header myself. I must use the client.Options.AddSubProtocol("my JWT") instead.

Upvotes: 3

Related Questions