/// <summary> /// Create a DeviceClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportSettings">Prioritized list of transportTypes and their settings</param> /// <returns>DeviceClient</returns> public static DeviceClient Create(string hostname, IAuthenticationMethod authenticationMethod, [System.Runtime.InteropServices.WindowsRuntime.ReadOnlyArrayAttribute] ITransportSettings[] transportSettings) { if (hostname == null) { throw new ArgumentNullException("hostname"); } if (authenticationMethod == null) { throw new ArgumentNullException("authenticationMethod"); } var connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, authenticationMethod); #if !WINDOWS_UWP && !PCL && !NETMF if (authenticationMethod is DeviceAuthenticationWithX509Certificate) { if (connectionStringBuilder.Certificate == null) { throw new ArgumentException("certificate must be present in DeviceAuthenticationWithX509Certificate"); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportSettings))); } #endif return(CreateFromConnectionString(connectionStringBuilder.ToString(), transportSettings)); }
/// <summary> /// Create a DeviceClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportType">The transportType used (Http1 or Amqp)</param> /// <returns>DeviceClient</returns> public static DeviceClient Create(string hostname, IAuthenticationMethod authenticationMethod, TransportType transportType) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } var connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, authenticationMethod); #if !WINDOWS_UWP && !PCL && !NETMF if (authenticationMethod is DeviceAuthenticationWithX509Certificate) { if (connectionStringBuilder.Certificate == null) { throw new ArgumentException("certificate must be present in DeviceAuthenticationWithX509Certificate"); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType))); } #endif return(CreateFromConnectionString(connectionStringBuilder.ToString(), transportType)); }
/// <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="transportSettings">Prioritized list of transportTypes and their settings</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, ITransportSettings[] transportSettings, ClientOptions options = default) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } var 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, transportSettings), options); dc.Certificate = connectionStringBuilder.Certificate; return(dc); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportSettings, 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)); }
private static async Task <string> BuildConnectionStringAsync(RegistryManager registryManager, DeviceItem deviceItem, TraceWriter log) { var iotHubConnectionString = IotHubConnectionStringBuilder.Create(Environment.GetEnvironmentVariable("IoTHubConnectionString")); var device = await registryManager.GetDeviceAsync(deviceItem.Name); var deviceKeyInfo = new DeviceAuthenticationWithRegistrySymmetricKey(deviceItem.Name, device.Authentication.SymmetricKey.PrimaryKey); var deviceConnectionString = ClientIotHubConnectionStringBuilder.Create(iotHubConnectionString.HostName, deviceKeyInfo); return(deviceConnectionString.ToString()); }
/// <summary> /// Create a DeviceClient from individual parameters /// </summary> /// <param name="hostname">The fully-qualified DNS hostname of IoT Hub</param> /// <param name="authenticationMethod">The authentication method that is used</param> /// <param name="transportType">The transportType used (Http1 or Amqp)</param> /// <returns>DeviceClient</returns> public static DeviceClient Create(string hostname, IAuthenticationMethod authenticationMethod, TransportType transportType) { if (hostname == null) { throw new ArgumentNullException("hostname"); } if (authenticationMethod == null) { throw new ArgumentNullException("authMethod"); } var connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, authenticationMethod); return(CreateFromConnectionString(connectionStringBuilder.ToString(), transportType)); }
/// <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)); }
/// <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="transportSettings">Prioritized list of transportTypes and their settings</param> /// <param name="options">The options that allow configuration of the device client instance during initialization.</param> /// <returns>InternalClient</returns> internal static InternalClient Create( string hostname, string gatewayHostname, IAuthenticationMethod authenticationMethod, ITransportSettings[] transportSettings, ClientOptions options = default) { if (hostname == null) { throw new ArgumentNullException(nameof(hostname)); } if (authenticationMethod == null) { throw new ArgumentNullException(nameof(authenticationMethod)); } if (!string.IsNullOrWhiteSpace(options?.ModelId) && transportSettings.Any(x => x.GetTransportType() == TransportType.Http1)) { throw new InvalidOperationException("Plug and Play is not supported over the HTTP transport."); } var 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, transportSettings), options); dc.Certificate = connectionStringBuilder.Certificate; return(dc); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportSettings, null, options)); }
public static IotHubConnectionString Parse(string connectionString) { var builder = IotHubConnectionStringBuilder.Create(connectionString); return(builder.ToIotHubConnectionString()); }
static async Task Main(string[] args) { var interval = 1; var connectionString = ""; var registryManager = RegistryManager.CreateFromConnectionString(connectionString); var numberOfDevices = Enumerable.Range(1, 50); var devices = new ConcurrentBag <DeviceItem>(); await ForEachAsync(numberOfDevices, 10, async (deviceNumber) => { var deviceName = $"SimulatedFridge-{deviceNumber:0000}"; Console.WriteLine($"Registering device ({deviceName})"); var device = await registryManager.GetDeviceAsync(deviceName); if (device == null) { device = await registryManager.AddDeviceAsync(new Device(deviceName)); } devices.Add(new DeviceItem() { Id = deviceName, }); var twin = new Twin() { Tags = { ["IsSimulated"] = "Y" } }; await registryManager.UpdateTwinAsync(device.Id, twin, "*"); }); await ForEachAsync(numberOfDevices, 10, async (deviceNumber) => { var deviceName = $"SimulatedLightBulbs-{deviceNumber:0000}"; Console.WriteLine($"Registering device ({deviceName})"); var device = await registryManager.GetDeviceAsync(deviceName); if (device == null) { device = await registryManager.AddDeviceAsync(new Device(deviceName)); } devices.Add(new DeviceItem() { Id = deviceName, }); var twin = new Twin() { Tags = { ["IsSimulated"] = "Y" } }; await registryManager.UpdateTwinAsync(device.Id, twin, "*"); }); await ForEachAsync(devices, 100, async (deviceItem) => { var device = await registryManager.GetDeviceAsync(deviceItem.Id); var iotHubConnectionString = IotHubConnectionStringBuilder.Create(connectionString); var deviceKeyInfo = new DeviceAuthenticationWithRegistrySymmetricKey(deviceItem.Id, device.Authentication.SymmetricKey.PrimaryKey); var deviceConnectionStringBuilder = ClientIotHubConnectionStringBuilder.Create(iotHubConnectionString.HostName, deviceKeyInfo); var deviceConnectionString = deviceConnectionStringBuilder.ToString(); var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString); await deviceClient.OpenAsync(); var message = FetchClientMessage(deviceItem); while (true) { await deviceClient.SendEventAsync(message); Console.WriteLine($"Sent data for ({deviceItem.Id})"); await Task.Delay(interval * 1000); message = FetchClientMessage(deviceItem); } }); await Task.Delay(1000); Console.ReadKey(); }
/// <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> internal 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 certificate && certificate.ChainCertificates != null) { throw new ArgumentException("Certificate chains are only supported on Amqp_Tcp_Only and Mqtt_Tcp_Only"); } if (!string.IsNullOrWhiteSpace(options?.ModelId) && transportType == TransportType.Http1) { throw new InvalidOperationException("Plug and Play is not supported over the HTTP transport."); } var 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 internalClient = CreateFromConnectionString( connectionStringBuilder.ToString(), authenticationMethod, PopulateCertificateInTransportSettings(connectionStringBuilder, transportType), null, options); internalClient.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(internalClient); } return(CreateFromConnectionString(connectionStringBuilder.ToString(), authenticationMethod, transportType, null, options)); }