public async Task <object> GetDeviceModules(string email, string subid, string resourceGroupName, string resourceName, string deviceId) { string access_token = _tokenDto.GetTokenString(email, _tokenResource.Value.manage); IoTHubKeys ioTHubKeys = await this._ioTHubResourceDto.GetIoTHubKeys(subid, resourceGroupName, resourceName, access_token); IoTHubInfoModel ioTHubInfoModel = await this._ioTHubResourceDto.GetIoTHubInfo(subid, resourceGroupName, resourceName, access_token); AccessPolicyModel accessPolicyModel = new AccessPolicyModel() { HostName = ioTHubInfoModel.properties.hostName, SharedAccessKeyName = ioTHubKeys.value[0].keyName, SharedAccessKey = ioTHubKeys.value[0].primaryKey }; return(await this._ioTHubResourceDto.GetDeviceModules(deviceId, accessPolicyModel, access_token)); }
public async Task <IoTHubKeys> GetIoTHubKeys(string subid, string resourceGroupName, string resourceName, string access_token) { string url = string.Format("{0}/resourceGroups/{1}/providers/Microsoft.Devices/IotHubs/" + "{2}/listkeys?api-version=2018-04-01", subid, resourceGroupName, resourceName); var request = new HttpRequestMessage(HttpMethod.Post, url); var client = this._clientFactory.CreateClient("chinacloudapi"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token); var response = await client.SendAsync(request); string data = await response.Content.ReadAsStringAsync(); IoTHubKeys ioTHubKeys = null; if (response.IsSuccessStatusCode) { ioTHubKeys = JsonConvert.DeserializeObject <IoTHubKeys>(data); } else { Console.WriteLine("Error." + response.ReasonPhrase); } return(ioTHubKeys); }