private static async Task <IoTHubResponse> EnrollToIoTHubAsync(string beckmanConnectId) { try { var primaryKey = ComputeDerivedSymmetricKey(Guid.NewGuid().ToString()); var secondKey = ComputeDerivedSymmetricKey(Guid.NewGuid().ToString()); // enroll to dps var enrollToDpsResult = await EnrollToDpsAsync(primaryKey, secondKey, beckmanConnectId, ProvisionConnString); if (!string.IsNullOrWhiteSpace(enrollToDpsResult.RegistrationId)) { // register in DPS, but not assign to IoTHub if (enrollToDpsResult.RegistrationState == null || string.IsNullOrWhiteSpace(enrollToDpsResult.RegistrationState.AssignedHub)) { // assign to IoTHub // TODO // If DPS success and IotHub failed, when customer register next time, will cause different keys for DPS and IoT // So need to get dps attestation in order to keep DPS and IoT have same keys. // For now, the DPS SDK cannot get symmetric keys text, consider to save in database or failure mode. var registerToIoTHub = await RegisterToIoTHubAsync(primaryKey, secondKey, beckmanConnectId); if (!string.IsNullOrWhiteSpace(registerToIoTHub.AssignedHub)) { // get device connection string returns to sync client var devicePrimaryConnectionString = $"HostName={registerToIoTHub.AssignedHub};DeviceId={beckmanConnectId};SharedAccessKey={primaryKey}"; var deviceSecondConnectionString = $"HostName={registerToIoTHub.AssignedHub};DeviceId={beckmanConnectId};SharedAccessKey={secondKey}"; var response = new IoTHubResponse { PrimaryKey = devicePrimaryConnectionString, SecondKey = deviceSecondConnectionString }; Console.WriteLine("Enroll successfully!"); return(response); } Console.WriteLine("Assign to IoTHub failed"); } else { Console.Write("Enroll successfully."); } } else { Console.WriteLine("Enroll to dps failed"); } } catch (Exception ex) { Console.WriteLine($"Exceptions: {ex}"); } return(null); }
private static async Task <IoTHubResponse> UpdateExistDeviceKeysAsync(string beckmanConnectId, string iotHubConnectionString) { using var provisioningServiceClient = ProvisioningServiceClient.CreateFromConnectionString(ProvisionConnString); var individualEnrollmentResult = await provisioningServiceClient.GetIndividualEnrollmentAsync(beckmanConnectId); var primaryKey = ComputeDerivedSymmetricKey(Guid.NewGuid().ToString()); var secondKey = ComputeDerivedSymmetricKey(Guid.NewGuid().ToString()); // update dps keys Attestation attestation = new SymmetricKeyAttestation(primaryKey, secondKey); individualEnrollmentResult.Attestation = attestation; var updateDpsResult = await provisioningServiceClient.CreateOrUpdateIndividualEnrollmentAsync(individualEnrollmentResult); if (updateDpsResult.RegistrationId == beckmanConnectId) { // update IoTHub keys (rollback or errors when failed) var registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); var device = await registryManager.GetDeviceAsync(beckmanConnectId); var newDevice = new Device(beckmanConnectId) { ETag = device.ETag, Authentication = new AuthenticationMechanism { SymmetricKey = new SymmetricKey { PrimaryKey = primaryKey, SecondaryKey = secondKey } } }; var deviceResponse = await registryManager.UpdateDeviceAsync(newDevice); if (string.IsNullOrWhiteSpace(deviceResponse.Id)) { var response = new IoTHubResponse { PrimaryKey = primaryKey, SecondKey = secondKey }; return(response); } } return(null); }