question

kishorekumar-4126 avatar image
0 Votes"
kishorekumar-4126 asked kishorekumar-4126 edited

dotnet core ClientWebSocket how to handle 401 Unauthorized

Dear Experts,

Please help to handle me below scenario.

I am trying to connect web socket server using dotnet core ClientWebSocket client.

Server authenticates the client using username & password.

  1. From client side (using ClientWebSocket client) how I can set username & password & send request to websocket Server.

  2. When ClientWebSocket client makes a request, if server failed authentication, server returns 401 Unauthorized code with WWW-Authenticate: BASIC header.

  3. In case 2, I need to send the ClientWebSocket request with following header

    Authorization: Basic 'base64 encoded of <user name>+":"+<password>'

How can I achieve with ClientWebSocket client in dotnet core.

Thanks,
Kumar

dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

A web socket is a persistent connection between a client and socket server. The first request must always send the credentials in order to establish the connection.

 string auth = Convert.ToBase64String(Encoding.Default.GetBytes(userName + ":" + password));
 client.Options.SetRequestHeader("Authorization", "Basic " + auth);


ClientWebSocketOptions
ClientWebSocket


1 Vote 1 ·

Dear AgaveJoe,

Thank you very much for your help.

I already tried this approach.
But in my case, there is already a Authorization header is filled send as a part of request for some other purpose.
In my case I must check the websocket response header status code = 401 & response header WWW-Authenticate: BASIC should exits.
In this case only I must send request header ("Authorization", "Basic " + ...).

Is there a possibilities to get websocket response headers & response status code.

0 Votes 0 ·

@kishorekumar-4126
As far as I know, there seems to be no way to get the status code directly from ClientWebSocket.
Will there be any related information in the error message? If there is, we can extract it from it.

0 Votes 0 ·
Show more comments

1 Answer

kishorekumar-4126 avatar image
0 Votes"
kishorekumar-4126 answered kishorekumar-4126 rolled back

Dear @TimonYang-MSFT ,

Thank you for your reply. In the error message I am not getting 'WWW-Authenticate: BASIC' information.

My development environment is Ubuntu 20.04 with dotnet 3.1.

I tried one more approach to set the websocket response headers & response status code.

https://stackoverflow.com/questions/22635663/setting-user-agent-http-header-in-clientwebsocket

Implemented a class ClientWebSocket2 derived from WebSocket like, public sealed class ClientWebSocket2 : WebSocket .

when I tried to instantiate class ClientWebSocket2, I am getting the following error.

124127-websocket-error.png

Is this approach is valid, if not please suggest another approach.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.