public async Task <string> ProvisionDevice()
        {
            string result    = string.Empty;
            string logPrefix = "DPSProvisioningService.ProvisionDevice".BuildLogPrefix();

            // When registering with a symmetric key using a group enrollment, the provided key will not
            // work for a specific device, rather it must be computed based on two values: the group enrollment
            // key and the desired device Id.
            if (_dpsSettings.EnrollmentType == EnrollmentType.Group)
            {
                if (_dpsSettings.GroupEnrollment == null)
                {
                    throw new ArgumentNullException("_dpsSettings.GroupEnrollment", "No group enrollment settings have been found.");
                }

                if (_dpsSettings.GroupEnrollment.SecurityType == SecurityType.SymmetricKey)
                {
                    _dpsSettings.GroupEnrollment.SymmetricKeySettings.PrimaryKey = ProvisioningTools.ComputeDerivedSymmetricKey(_dpsSettings.GroupEnrollment.SymmetricKeySettings.PrimaryKey, _deviceSettingsDelegate.CurrentValue.DeviceId);
                }
            }

            _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Initializing the device provisioning client...");

            // For individual enrollments, the first parameter must be the registration Id, where in the enrollment
            // the device Id is already chosen. However, for group enrollments the device Id can be requested by
            // the device, as long as the key has been computed using that value.
            // Also, the secondary could could be included, but was left out for the simplicity of this sample.
            using (var security = new SecurityProviderSymmetricKey(_deviceSettingsDelegate.CurrentValue.DeviceId, _dpsSettings.GroupEnrollment.SymmetricKeySettings.PrimaryKey, null))
            {
                using (var transportHandler = ProvisioningTools.GetTransportHandler(_dpsSettings.GroupEnrollment.SymmetricKeySettings.TransportType))
                {
                    ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(
                        _dpsSettings.GroupEnrollment.SymmetricKeySettings.GlobalDeviceEndpoint,
                        _dpsSettings.GroupEnrollment.SymmetricKeySettings.IdScope,
                        security,
                        transportHandler);

                    _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Initialized for registration Id {security.GetRegistrationID()}.");
                    _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Registering with the device provisioning service...");

                    DeviceRegistrationResult deviceRegistrationResult = await provClient.RegisterAsync();

                    if (deviceRegistrationResult != null)
                    {
                        _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Registration status: {deviceRegistrationResult.Status}.");
                        if (deviceRegistrationResult.Status != ProvisioningRegistrationStatusType.Assigned)
                        {
                            _logger.LogWarning($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Device registration process did not assign an IoT Hub.");
                        }
                        else
                        {
                            _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Device {deviceRegistrationResult.DeviceId} registered to {deviceRegistrationResult.AssignedHub}.");
                            _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Creating symmetric key authentication for IoT Hub...");

                            var devicePrimaryKey = security.GetPrimaryKey();
                            //IAuthenticationMethod auth = new DeviceAuthenticationWithRegistrySymmetricKey(deviceRegistrationResult.DeviceId, devicePrimaryKey);

                            //_logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::Testing the provisioned device with IoT Hub...");

                            //using (DeviceClient iotClient = DeviceClient.Create(deviceRegistrationResult.AssignedHub, auth, _dpsSettings.GroupEnrollment.SymmetricKeySettings.TransportType))
                            //{
                            //    _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::Sending a telemetry message after provisioning to test the process...");

                            //    using var message = new Message(Encoding.UTF8.GetBytes("TestMessage"));
                            //    await iotClient.SendEventAsync(message);

                            //    _logger.LogDebug($"{logPrefix}::{_deviceSettings.ArtifactId}::Finished.");
                            //}

                            result = $"HostName={deviceRegistrationResult.AssignedHub};DeviceId={deviceRegistrationResult.DeviceId};SharedAccessKey={devicePrimaryKey}";
                        }
                    }
                    else
                    {
                        _logger.LogError($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::No provisioning result has been received.");
                    }
                }
            }

            return(result);
        }
        public async Task <string> ProvisionDevice()
        {
            string result    = string.Empty;
            string logPrefix = "DPSProvisioningService.ProvisionDevice".BuildLogPrefix();

            // When registering with a symmetric key using a group enrollment, the provided key will not
            // work for a specific device, rather it must be computed based on two values: the group enrollment
            // key and the desired device Id.
            if (_dpsSettings.EnrollmentType == EnrollmentType.Group)
            {
                if (_dpsSettings.GroupEnrollment == null)
                {
                    throw new ArgumentNullException("_dpsSettings.GroupEnrollment", "No group enrollment settings have been found.");
                }

                if (_dpsSettings.GroupEnrollment.SecurityType == SecurityType.X509CA && _dpsSettings.GroupEnrollment.CAX509Settings != null)
                {
                    string deviceCertificateFullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _dpsSettings.GroupEnrollment.CAX509Settings.DeviceX509Path);

                    // X509 device leaf certificate
                    string parameterDeviceId = _deviceSettingsDelegate.CurrentValue.ArtifactId;

                    X509Certificate2 deviceLeafProvisioningCertificate = new X509Certificate2(deviceCertificateFullPath, _dpsSettings.GroupEnrollment.CAX509Settings.Password);
                    _deviceSettingsDelegate.CurrentValue.DeviceId = deviceLeafProvisioningCertificate.Subject.Remove(0, 3); //delete the 'CN='

                    if (parameterDeviceId != _deviceSettingsDelegate.CurrentValue.DeviceId)
                    {
                        _logger.LogWarning($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::The leaf certificate has been created for the device '{_deviceSettingsDelegate.CurrentValue.DeviceId}' but the provided deviceId through the settings is '{parameterDeviceId}'. Provisioning will be executed with the deviceId included in the certificate.");
                    }

                    _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Initializing the device provisioning client...");

                    using (var security = new SecurityProviderX509Certificate(deviceLeafProvisioningCertificate))
                    {
                        using (var transportHandler = ProvisioningTools.GetTransportHandler(_dpsSettings.GroupEnrollment.CAX509Settings.TransportType))
                        {
                            ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(
                                _dpsSettings.GroupEnrollment.CAX509Settings.GlobalDeviceEndpoint,
                                _dpsSettings.GroupEnrollment.CAX509Settings.IdScope,
                                security,
                                transportHandler);

                            _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Initialized for registration Id {security.GetRegistrationID()}.");
                            _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Registering with the device provisioning service...");

                            DeviceRegistrationResult deviceRegistrationResult = await provClient.RegisterAsync();

                            if (deviceRegistrationResult != null)
                            {
                                _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Registration status: {deviceRegistrationResult.Status}.");
                                if (deviceRegistrationResult.Status != ProvisioningRegistrationStatusType.Assigned)
                                {
                                    _logger.LogWarning($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Device registration process did not assign an IoT Hub.");
                                }
                                else
                                {
                                    _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Device {deviceRegistrationResult.DeviceId} registered to {deviceRegistrationResult.AssignedHub}.");
                                    _logger.LogDebug($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::Creating X509 leaf authentication for IoT Hub...");

                                    _deviceSettingsDelegate.CurrentValue.DeviceId = deviceRegistrationResult.DeviceId;

                                    //HostName =< host_name >; DeviceId =< device_id >; x509 = true
                                    result = $"HostName={deviceRegistrationResult.AssignedHub};DeviceId={deviceRegistrationResult.DeviceId};x509=true";
                                }
                            }
                            else
                            {
                                _logger.LogError($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::No provisioning result has been received.");
                            }
                        }
                    }
                }
                else
                {
                    _logger.LogError($"{logPrefix}::{_deviceSettingsDelegate.CurrentValue.ArtifactId}::No X509 settings have been found.");
                }
            }

            return(result);
        }