public byte[] GetDeviceProvisioningQrCode(PhoneClientData data, int height, int width, int margin)
        {
            var qrCodeWriter = new ZXing.BarcodeWriterPixelData
            {
                Format  = ZXing.BarcodeFormat.QR_CODE,
                Options = new QrCodeEncodingOptions {
                    Height = height, Width = width, Margin = margin
                }
            };

            var pixelData = qrCodeWriter.Write(JsonConvert.SerializeObject(data));

            using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
                using (var memoryStream = new MemoryStream())
                {
                    var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
                                                     ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
                    try
                    {
                        // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
                        System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
                                                                    pixelData.Pixels.Length);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    // save to stream as PNG
                    bitmap.Save(memoryStream, ImageFormat.Png);
                    return(memoryStream.ToArray());
                }
        }
        public IActionResult GetProvisioningQrCode(string nonce = null, int height = 300, int width = 300, int margin = 0)
        {
            string queryParams = string.Empty;

            if (!String.IsNullOrEmpty(nonce))
            {
                queryParams = $"?nonce={nonce}";
            }

            var data = new PhoneClientData
            {
                BaseUrl           = $"{this.Request.Scheme}://{this.Request.Host}",
                RegistrationUrl   = $"{this.Request.Scheme}://{this.Request.Host}/api/devices{queryParams}",
                RegistrationToken = this.tokenService.GetShortLivedDeviceRegistrationToken()
            };

            return(File(
                       this.deviceService.GetDeviceProvisioningQrCode(
                           data,
                           height,
                           width,
                           margin),
                       "image/png"));
        }