From 510d6fd21f3406b68691b4fb34268a1945ab0437 Mon Sep 17 00:00:00 2001 From: Hamza Lazaar Date: Fri, 12 Jun 2026 12:03:56 +0200 Subject: [PATCH] [Modify] Expose method to set Authorization request header to a string value --- websocket-sharp/WebSocket.cs | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 3ba985774..f6745dbb1 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -73,6 +73,7 @@ public class WebSocket : IDisposable #region Private Fields private AuthenticationChallenge _authChallenge; + private string _authHeaderValue; private string _base64Key; private bool _client; private Action _closeContext; @@ -1683,6 +1684,9 @@ private HttpRequest createHandshakeRequest () if (ares != null) headers["Authorization"] = ares.ToString (); + if (!_authHeaderValue.IsNullOrEmpty ()) + headers["Authorization"] = _authHeaderValue; + var hasUserHeader = _userHeaders != null && _userHeaders.Count > 0; if (hasUserHeader) @@ -4550,6 +4554,72 @@ public void SetUserHeader (string name, string value) } } + /// + /// Sets the Authorization request header to a specified string value. + /// + /// + /// A that specifies the value of the Authorization request header to set. + /// + /// + /// contains an invalid character. + /// + /// + /// The length of is greater than 65,535 + /// characters. + /// + /// + /// + /// This method is not available if the interface is not for the client. + /// + /// + /// -or- + /// + /// + /// This method is not available when the current state of the interface + /// is neither New nor Closed. + /// + /// + public void SetAuthorizationHeader (string value) + { + if (!_client) + { + const string msg = "The interface is not for the client."; + + throw new InvalidOperationException (msg); + } + + var val = value != null ? value.Trim () : string.Empty; + + if (val.Length > 0) + { + if (val.Length > 65535) + { + const string msg = "The length of that value is greater than 65,535 characters."; + + throw new ArgumentOutOfRangeException (nameof(value), msg); + } + + if (!val.IsText ()) + { + const string msg = "That value contains an invalid character."; + + throw new ArgumentException (msg, nameof(value)); + } + } + + lock (_forState) + { + if (!canSet ()) + { + const string msg = "The current state of the interface is neither New nor Closed."; + + throw new InvalidOperationException (msg); + } + + _authHeaderValue = val.Length > 0 ? val : null; + } + } + #endregion #region Explicit Interface Implementations