Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
466 stars 493 forks source link

Exception while trying to enroll the simulated device #1067

Closed IdanAviad closed 5 years ago

IdanAviad commented 5 years ago

Hello, I’m trying to follow the IOT Provisioning Quickstarts tutorial (Create and provision a simulated TPM device using C# device SDK for IoT Hub Device Provisioning Service) I keep getting the following exception while trying to enroll the simulated device. Can you please help me figuring what is the problem? I’m pretty sure that I entered a valid Endorsement key and Registration ID as displayed in the example command widow.

The Exception I get:

Microsoft.Azure.Devices.Provisioning.Client.ProvisioningTransportException HResult=0x80131500 Message=HTTP transport exception: malformed server error message: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Certificate verification error

Certificate verification error

Overview:

Access denied. Certificate validation failed for the following URL: global.azure-devices-provisioning.net

Details:

VERIFY DENY: depth=0, Client Certificate requested: "global.azure-devices-provisioning.net"

Options:

Pressing the button allows you to visit the website although the certificate is not valid.

Click to return to the previous page:

To find out more about why access was denied, please contact your administrator. The reference ID is 148.

' Source=Microsoft.Azure.Devices.Provisioning.Transport.Http StackTrace: at Microsoft.Azure.Devices.Provisioning.Client.Transport.ProvisioningTransportHandlerHttp.d__3.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at Microsoft.Azure.Devices.Provisioning.Client.Samples.ProvisioningDeviceClientSample.d__3.MoveNext() in C:\IoT\azure-iot-samples-csharp-master\provisioning\Samples\device\Common\ProvisioningDeviceClientSample.cs:line 31 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Devices.Provisioning.Client.Samples.Program.Main(String[] args) in C:\IoT\azure-iot-samples-csharp-master\provisioning\Samples\device\TpmSample\Program.cs:line 71 Inner Exception 1: JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
timtay-microsoft commented 5 years ago

Can you please provide the code that you are using that gives you this exception? And can you share more information about the device that is running it? Is this windows, linux, etc? What versions of the provisioning dependencies are you using?

IdanAviad commented 5 years ago

Hi, This is the code sample from the Azure IoT Samples for C# to simulate a TPM device on a development machine running the Windows OS: public static int Main(string[] args) { if (string.IsNullOrWhiteSpace(s_idScope) && (args.Length > 0)) { s_idScope = args[0]; }

        if (string.IsNullOrWhiteSpace(s_idScope))
        {
            Console.WriteLine("ProvisioningDeviceClientTpm <IDScope>");
            return 1;
        }

        // Remove if a real TPM is being used.
        Console.WriteLine("Starting TPM simulator.");
        SecurityProviderTpmSimulator.StartSimulatorProcess();

        // Replace the following type with SecurityProviderTpmHsm() to use a real TPM2.0 device.
        using (var security = new SecurityProviderTpmSimulator(RegistrationId))

        // Select one of the available transports:
        // To optimize for size, reference only the protocols used by your application.
        using (var transport = new ProvisioningTransportHandlerHttp())
        // using (var transport = new ProvisioningTransportHandlerAmqp(TransportFallbackType.TcpOnly))
        // using (var transport = new ProvisioningTransportHandlerAmqp(TransportFallbackType.WebSocketOnly))
        {
            // Note that the TPM simulator will create an NVChip file containing the simulated TPM state.
            Console.WriteLine("Extracting endorsement key.");
            string base64EK = Convert.ToBase64String(security.GetEndorsementKey());

            Console.WriteLine(
                "In your Azure Device Provisioning Service please go to 'Manage enrollments' and select " +
                "'Individual Enrollments'. Select 'Add' then fill in the following:");

            Console.WriteLine("\tMechanism: TPM");
            Console.WriteLine($"\tRegistration ID: {RegistrationId}");
            Console.WriteLine($"\tEndorsement key: {base64EK}");
            Console.WriteLine("\tDevice ID: iothubtpmdevice1 (or any other valid DeviceID)");
            Console.WriteLine();
            Console.WriteLine("Press ENTER when ready.");
            Console.ReadLine();

            ProvisioningDeviceClient provClient =
                ProvisioningDeviceClient.Create(GlobalDeviceEndpoint, s_idScope, security, transport);

            var sample = new ProvisioningDeviceClientSample(provClient, security);
            sample.RunSampleAsync().GetAwaiter().GetResult();   //This is were I get the exception
        }

        return 0;
    }

The Device is windows 10! What do you mean "Versions of the provisioning dependencies"? and were can I find it?

Thanks! Idan

timtay-microsoft commented 5 years ago

When you add a nuget dependency to get the ProvisioningDeviceClient, you have to specify which version of the dependency you want. You can check your nuget package manager to find out which version you are using

IdanAviad commented 5 years ago

Hi, I updated to the latest versions: Microsoft.Azure.Devices.Client: V1.21.1 Microsoft.Azure.Devices.Provisioning.Client: V1.4.0 Microsoft.Azure.Devices.Provisioning.Security.Tpm: V1.1.6 Microsoft.Azure.Devices.Provisioning.Transport.Amqp: V1.1.9 Microsoft.Azure.Devices.Provisioning.Transport.Http: V1.1.6 Microsoft.Azure.Devices.Provisioning.Transport.Mqtt: V1.1.8

Still, I get the same exception :(

Finally, I changed the transport layer from Http to Amqp and it worked! :) Why it doesn't work with Http? I do not know.

az-iot-builder-01 commented 5 years ago

@IdanAviad, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey