public async Task <List <IoTHubDeviceInfo> > GetDeviceList(string devEUI, string gatewayId, string devNonce, string devAddr) { var results = new List <IoTHubDeviceInfo>(); if (devEUI != null) { // OTAA join string cacheKey = devEUI + devNonce; using (var deviceCache = new LoRaDeviceCache(this.cacheStore, devEUI, gatewayId, cacheKey)) { if (deviceCache.HasValue()) { throw new DeviceNonceUsedException(); } if (deviceCache.TryToLock(cacheKey + "joinlock")) { if (deviceCache.HasValue()) { throw new DeviceNonceUsedException(); } deviceCache.SetValue(devNonce, TimeSpan.FromMinutes(1)); var device = await this.registryManager.GetDeviceAsync(devEUI); if (device != null) { var iotHubDeviceInfo = new IoTHubDeviceInfo { DevEUI = devEUI, PrimaryKey = device.Authentication.SymmetricKey.PrimaryKey }; results.Add(iotHubDeviceInfo); this.cacheStore.KeyDelete(devEUI); } } else { throw new DeviceNonceUsedException(); } } } else if (devAddr != null) { // ABP or normal message // TODO check for sql injection devAddr = devAddr.Replace('\'', ' '); var query = this.registryManager.CreateQuery($"SELECT * FROM devices WHERE properties.desired.DevAddr = '{devAddr}' OR properties.reported.DevAddr ='{devAddr}'", 100); while (query.HasMoreResults) { var page = await query.GetNextAsTwinAsync(); foreach (var twin in page) { if (twin.DeviceId != null) { var device = await this.registryManager.GetDeviceAsync(twin.DeviceId); var iotHubDeviceInfo = new IoTHubDeviceInfo { DevAddr = devAddr, DevEUI = twin.DeviceId, PrimaryKey = device.Authentication.SymmetricKey.PrimaryKey }; results.Add(iotHubDeviceInfo); } } } } else { throw new Exception("Missing devEUI or devAddr"); } return(results); }