mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
UseDefaultCredentials options for WebSockets proxy configuration. (#1734)
This commit is contained in:
@@ -2,15 +2,19 @@
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace MQTTnet.Client
|
||||
{
|
||||
public sealed class MqttClientWebSocketOptions : IMqttClientChannelOptions
|
||||
{
|
||||
public CookieContainer CookieContainer { get; set; }
|
||||
|
||||
|
||||
public ICredentials Credentials { get; set; }
|
||||
|
||||
public MqttClientWebSocketProxyOptions ProxyOptions { get; set; }
|
||||
|
||||
public IDictionary<string, string> RequestHeaders { get; set; }
|
||||
@@ -18,12 +22,26 @@ namespace MQTTnet.Client
|
||||
public ICollection<string> SubProtocols { get; set; } = new List<string> { "mqtt" };
|
||||
|
||||
public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions();
|
||||
|
||||
|
||||
public string Uri { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Uri;
|
||||
}
|
||||
|
||||
#if !NETSTANDARD1_3
|
||||
/// <summary>
|
||||
/// Gets or sets the keep alive interval for the Web Socket connection.
|
||||
/// This is not related to the keep alive interval for the MQTT protocol.
|
||||
/// </summary>
|
||||
public TimeSpan KeepAliveInterval { get; set; } = WebSocket.DefaultKeepAliveInterval;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the default (system) credentials should be used when connecting via Web Socket connection.
|
||||
/// This is not related to the credentials which are used for the MQTT protocol.
|
||||
/// </summary>
|
||||
public bool UseDefaultCredentials { get; set; }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ namespace MQTTnet.Client
|
||||
|
||||
public bool BypassOnLocal { get; set; }
|
||||
|
||||
public bool UseDefaultCredentials { get; set; }
|
||||
|
||||
public string[] BypassList { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -155,14 +155,26 @@ namespace MQTTnet.Implementations
|
||||
throw new NotSupportedException("Proxies are not supported when using 'netstandard 1.3'.");
|
||||
#else
|
||||
var proxyUri = new Uri(_options.ProxyOptions.Address);
|
||||
|
||||
WebProxy webProxy;
|
||||
|
||||
if (!string.IsNullOrEmpty(_options.ProxyOptions.Username) && !string.IsNullOrEmpty(_options.ProxyOptions.Password))
|
||||
{
|
||||
var credentials = new NetworkCredential(_options.ProxyOptions.Username, _options.ProxyOptions.Password, _options.ProxyOptions.Domain);
|
||||
return new WebProxy(proxyUri, _options.ProxyOptions.BypassOnLocal, _options.ProxyOptions.BypassList, credentials);
|
||||
webProxy = new WebProxy(proxyUri, _options.ProxyOptions.BypassOnLocal, _options.ProxyOptions.BypassList, credentials);
|
||||
}
|
||||
else
|
||||
{
|
||||
webProxy = new WebProxy(proxyUri, _options.ProxyOptions.BypassOnLocal, _options.ProxyOptions.BypassList);
|
||||
}
|
||||
|
||||
if (_options.ProxyOptions.UseDefaultCredentials)
|
||||
{
|
||||
// Only update the property if required because setting it to false will alter
|
||||
// the used credentials internally!
|
||||
webProxy.UseDefaultCredentials = true;
|
||||
}
|
||||
|
||||
return new WebProxy(proxyUri, _options.ProxyOptions.BypassOnLocal, _options.ProxyOptions.BypassList);
|
||||
return webProxy;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -206,7 +218,13 @@ namespace MQTTnet.Implementations
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if !NETSTANDARD1_3
|
||||
clientWebSocket.Options.UseDefaultCredentials = _options.UseDefaultCredentials;
|
||||
clientWebSocket.Options.KeepAliveInterval = _options.KeepAliveInterval;
|
||||
#endif
|
||||
clientWebSocket.Options.Credentials = _options.Credentials;
|
||||
|
||||
var certificateValidationHandler = _options.TlsOptions?.CertificateValidationHandler;
|
||||
if (certificateValidationHandler != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user