/// <summary> /// Create a InternalClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="gatewayHostname">The fully-qualified DNS hostname of Gateway</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportType">The transportType used (Http1, Amqp or Mqtt), <see cref="TransportType"/></param> /// <returns>InternalClient</returns> public static InternalClient Create(string hostname, string gatewayHostname, IAuthenticationMethod authenticationMethod, TransportType transportType) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod); if (authenticationMethod is DeviceAuthenticationWithX509Certificate) { if (connectionStringBuilder.Certificate == null) { throw new ArgumentException("certificate must be present in DeviceAuthenticationWithX509Certificate"); } InternalClient dc = CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType)); dc.Certificate = connectionStringBuilder.Certificate; return(dc); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null)); }
/// <summary> /// Create a InternalClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="gatewayHostname">The fully-qualified DNS hostname of Gateway</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportType">The transportType used (Http1, Amqp or Mqtt), <see cref="TransportType"/></param> /// <param name="options">The options that allow configuration of the device client instance during initialization.</param> /// <returns>InternalClient</returns> public static InternalClient Create(string hostname, string gatewayHostname, IAuthenticationMethod authenticationMethod, TransportType transportType, ClientOptions options = default) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod); // Make sure client options is initialized with the correct transport setting. EnsureOptionsIsSetup(connectionStringBuilder.Certificate, ref options); if (authenticationMethod is DeviceAuthenticationWithX509Certificate) { if (connectionStringBuilder.Certificate == null) { throw new ArgumentException("No certificate was found. To use certificate authentication certificate must be present."); } InternalClient dc = CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType), options); dc.Certificate = connectionStringBuilder.Certificate; return(dc); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null, options)); }
/// <summary> /// Create a InternalClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="gatewayHostname">The fully-qualified DNS hostname of Gateway</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportType">The transportType used (Http1, Amqp or Mqtt), <see cref="TransportType"/></param> /// <param name="options">The options that allow configuration of the device client instance during initialization.</param> /// <returns>InternalClient</returns> public static InternalClient Create(string hostname, string gatewayHostname, IAuthenticationMethod authenticationMethod, TransportType transportType, ClientOptions options = default) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } if (transportType != TransportType.Amqp_Tcp_Only && transportType != TransportType.Mqtt_Tcp_Only && authenticationMethod is DeviceAuthenticationWithX509Certificate && ((DeviceAuthenticationWithX509Certificate)authenticationMethod).ChainCertificates != null) { throw new ArgumentException("Certificate chains are only supported on Amqp_Tcp_Only and Mqtt_Tcp_Only"); } IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod); // Make sure client options is initialized with the correct transport setting. EnsureOptionsIsSetup(connectionStringBuilder.Certificate, ref options); if (authenticationMethod is DeviceAuthenticationWithX509Certificate) { if (connectionStringBuilder.Certificate == null) { throw new ArgumentException("No certificate was found. To use certificate authentication certificate must be present."); } #pragma warning disable CA2000 // This is returned to client so cannot be disposed here. InternalClient dc = CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType), options); #pragma warning restore CA2000 dc.Certificate = connectionStringBuilder.Certificate; // Install all the intermediate certificates in the chain if specified. if (connectionStringBuilder.ChainCertificates != null) { try { CertificateInstaller.EnsureChainIsInstalled(connectionStringBuilder.ChainCertificates); } catch (Exception ex) { if (Logging.IsEnabled) { Logging.Error(null, $"{nameof(CertificateInstaller)} failed to read or write to cert store due to: {ex}"); } throw new UnauthorizedException($"Failed to provide certificates in the chain - {ex.Message}", ex); } } return(dc); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null, options)); }
/// <summary> /// Create a InternalClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="gatewayHostname">The fully-qualified DNS hostname of Gateway</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportType">The transportType used (Http1, Amqp or Mqtt), <see cref="TransportType"/></param> /// <param name="options">The options that allow configuration of the device client instance during initialization.</param> /// <returns>InternalClient</returns> public static InternalClient Create(string hostname, string gatewayHostname, IAuthenticationMethod authenticationMethod, TransportType transportType, ClientOptions options = default) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod); if (authenticationMethod is DeviceAuthenticationWithX509Certificate) { if (connectionStringBuilder.Certificate == null) { throw new ArgumentException("certificate must be present in DeviceAuthenticationWithX509Certificate"); } // If the file upload transport settings hasn't been specified, we will create one using the client certificate on the connection string if (options?.FileUploadTransportSettings == null) { var fileUploadTransportSettings = new Http1TransportSettings { ClientCertificate = connectionStringBuilder.Certificate }; if (options == null) { options = new ClientOptions { FileUploadTransportSettings = fileUploadTransportSettings }; } else { options.FileUploadTransportSettings = fileUploadTransportSettings; } } InternalClient dc = CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType), options); dc.Certificate = connectionStringBuilder.Certificate; return(dc); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null, options)); }
internal static IAuthenticationMethod GetAuthenticationMethod(IotHubConnectionStringBuilder iotHubConnectionStringBuilder) { if (iotHubConnectionStringBuilder.SharedAccessKeyName != null) { return(new DeviceAuthenticationWithSharedAccessPolicyKey( iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.SharedAccessKeyName, iotHubConnectionStringBuilder.SharedAccessKey)); } else if (iotHubConnectionStringBuilder.SharedAccessKey != null) { #if !NETMF if (iotHubConnectionStringBuilder.ModuleId != null) { return(new ModuleAuthenticationWithRegistrySymmetricKey( iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.ModuleId, iotHubConnectionStringBuilder.SharedAccessKey)); } else #endif { return(new DeviceAuthenticationWithRegistrySymmetricKey( iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.SharedAccessKey)); } } else if (iotHubConnectionStringBuilder.SharedAccessSignature != null) { #if !NETMF if (iotHubConnectionStringBuilder.ModuleId != null) { return(new ModuleAuthenticationWithToken( iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.ModuleId, iotHubConnectionStringBuilder.SharedAccessSignature)); } else #endif { return(new DeviceAuthenticationWithToken( iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.SharedAccessSignature)); } } #if !NETMF else if (iotHubConnectionStringBuilder.UsingX509Cert) { return(new DeviceAuthenticationWithX509Certificate(iotHubConnectionStringBuilder.DeviceId, iotHubConnectionStringBuilder.Certificate)); } #endif #if NETMF throw new InvalidOperationException("Unsupported Authentication Method " + iotHubConnectionStringBuilder.ToString()); #else throw new InvalidOperationException("Unsupported Authentication Method {0}".FormatInvariant(iotHubConnectionStringBuilder)); #endif }