Exemple #1
0
        public WebSocketsTransport(HttpOptions httpOptions, ILoggerFactory loggerFactory)
        {
            _webSocket = new ClientWebSocket();

            // Issue in ClientWebSocket prevents user-agent being set - https://github.com/dotnet/corefx/issues/26627
            //_webSocket.Options.SetRequestHeader("User-Agent", Constants.UserAgentHeader.ToString());

            if (httpOptions?.Headers != null)
            {
                foreach (var header in httpOptions.Headers)
                {
                    _webSocket.Options.SetRequestHeader(header.Key, header.Value);
                }
            }

            if (httpOptions?.AccessTokenFactory != null)
            {
                _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {httpOptions.AccessTokenFactory()}");
            }

            httpOptions?.WebSocketOptions?.Invoke(_webSocket.Options);

            _closeTimeout = httpOptions?.CloseTimeout ?? TimeSpan.FromSeconds(5);
            _logger       = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger <WebSocketsTransport>();
        }
Exemple #2
0
        public WebSocketsTransport(HttpOptions httpOptions, ILoggerFactory loggerFactory)
        {
            _webSocket = new ClientWebSocket();

            // Issue in ClientWebSocket prevents user-agent being set - https://github.com/dotnet/corefx/issues/26627
            //_webSocket.Options.SetRequestHeader("User-Agent", Constants.UserAgentHeader.ToString());

            if (httpOptions != null)
            {
                if (httpOptions.Headers != null)
                {
                    foreach (var header in httpOptions.Headers)
                    {
                        _webSocket.Options.SetRequestHeader(header.Key, header.Value);
                    }
                }

                if (httpOptions.Cookies != null)
                {
                    _webSocket.Options.Cookies = httpOptions.Cookies;
                }

                if (httpOptions.ClientCertificates != null)
                {
                    _webSocket.Options.ClientCertificates.AddRange(httpOptions.ClientCertificates);
                }

                if (httpOptions.Credentials != null)
                {
                    _webSocket.Options.Credentials = httpOptions.Credentials;
                }

                if (httpOptions.Proxy != null)
                {
                    _webSocket.Options.Proxy = httpOptions.Proxy;
                }

                if (httpOptions.UseDefaultCredentials != null)
                {
                    _webSocket.Options.UseDefaultCredentials = httpOptions.UseDefaultCredentials.Value;
                }

                if (httpOptions.AccessTokenFactory != null)
                {
                    _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {httpOptions.AccessTokenFactory()}");
                }

                httpOptions.WebSocketOptions?.Invoke(_webSocket.Options);

                _closeTimeout = httpOptions.CloseTimeout;
            }

            // Set this header so the server auth middleware will set an Unauthorized instead of Redirect status code
            // See: https://github.com/aspnet/Security/blob/ff9f145a8e89c9756ea12ff10c6d47f2f7eb345f/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs#L42
            _webSocket.Options.SetRequestHeader("X-Requested-With", "XMLHttpRequest");

            _logger = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger <WebSocketsTransport>();
        }
Exemple #3
0
        public WebSocketsTransport(HttpOptions httpOptions, ILoggerFactory loggerFactory)
        {
            _webSocket = new ClientWebSocket();

            // Issue in ClientWebSocket prevents user-agent being set - https://github.com/dotnet/corefx/issues/26627
            //_webSocket.Options.SetRequestHeader("User-Agent", Constants.UserAgentHeader.ToString());

            if (httpOptions != null)
            {
                if (httpOptions.Headers != null)
                {
                    foreach (var header in httpOptions.Headers)
                    {
                        _webSocket.Options.SetRequestHeader(header.Key, header.Value);
                    }
                }

                if (httpOptions.Cookies != null)
                {
                    _webSocket.Options.Cookies = httpOptions.Cookies;
                }

                if (httpOptions.ClientCertificates != null)
                {
                    _webSocket.Options.ClientCertificates.AddRange(httpOptions.ClientCertificates);
                }

                if (httpOptions.Credentials != null)
                {
                    _webSocket.Options.Credentials = httpOptions.Credentials;
                }

                if (httpOptions.Proxy != null)
                {
                    _webSocket.Options.Proxy = httpOptions.Proxy;
                }

                if (httpOptions.UseDefaultCredentials != null)
                {
                    _webSocket.Options.UseDefaultCredentials = httpOptions.UseDefaultCredentials.Value;
                }

                if (httpOptions.AccessTokenFactory != null)
                {
                    _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {httpOptions.AccessTokenFactory()}");
                }

                httpOptions.WebSocketOptions?.Invoke(_webSocket.Options);

                _closeTimeout = httpOptions.CloseTimeout;
            }

            _logger = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger <WebSocketsTransport>();
        }
Exemple #4
0
        public static void PrepareHttpRequest(HttpRequestMessage request, HttpOptions httpOptions)
        {
            if (httpOptions?.Headers != null)
            {
                foreach (var header in httpOptions.Headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
            request.Headers.UserAgent.Add(Constants.UserAgentHeader);

            if (httpOptions?.AccessTokenFactory != null)
            {
                request.Headers.Add("Authorization", $"Bearer {httpOptions.AccessTokenFactory()}");
            }
        }
Exemple #5
0
        public WebSocketsTransport(HttpOptions httpOptions, ILoggerFactory loggerFactory)
        {
            _webSocket = new ClientWebSocket();

            if (httpOptions?.Headers != null)
            {
                foreach (var header in httpOptions.Headers)
                {
                    _webSocket.Options.SetRequestHeader(header.Key, header.Value);
                }
            }

            if (httpOptions?.AccessTokenFactory != null)
            {
                _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {httpOptions.AccessTokenFactory()}");
            }

            httpOptions?.WebSocketOptions?.Invoke(_webSocket.Options);

            _logger = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger <WebSocketsTransport>();
        }