/// <summary> /// This method connects to the IoT Hub with the device identity. /// by that it queries the authentication information for its module authentication /// It then returns the Connection string by which the Module can perform authentication /// </summary> /// <returns>Connection String</returns> public override string GetConnectionString() { JToken registrationToken = _dpsApi.GetDeviceRegistration(_idScope, _registrationId); if (registrationToken == null) { throw new FailedToGetRegistrationException($"Registration ID: \"{_registrationId}\" for ID Scope: \"{_idScope}\" could not be retrieved"); } string assignedHub = registrationToken["assignedHub"].ToString(); string deviceId = registrationToken["deviceId"].ToString(); string status = registrationToken["status"].ToString(); if (status != "assigned") { // TODO: handle error } _deviceAuthenticationData.GatewayHostName = assignedHub; _deviceAuthenticationData.DeviceId = deviceId; IDeviceApi deviceApi = new DevicesApi(RestClient.CreateFrom(_deviceAuthenticationData)); JToken moduleJToken = deviceApi.GetDeviceAgentModule(deviceId); if (moduleJToken == null) { throw new FailedToGetModuleException($"Module for device: {deviceId} is empty"); } //Note that the response represents the object Module in the SDK Microsoft.Azure.Devices //We do not want to include the SDK in the project so we extract the specific parameters we need string primaryKey = moduleJToken["authentication"]["symmetricKey"]["primaryKey"].ToString(); return(CreateModuleConnectionString(assignedHub, deviceId, primaryKey)); }