Search code examples
azureazure-iot-hubiot-devkitiot-workbenchmxchip

Microsoft.Azure.Devices.Client.Exceptions.UnauthorizedException: 'CONNECT failed: RefusedNotAuthorized'


I have successfully configured my Azure IoT Dev Kit MXChip and I am able to send the data to the IoT Hub. I have also created a logic app with a router in it to receive a mail when the temperature is more than expected and a SQL server to save the Stream Analytics Job data. Basically, I followed the tutorial and till this point, everything was working fine, now I am just creating a simulator device where I can simulate events as mentioned in this tutorial. But whenever I run the application, I am always getting an error as below.

Microsoft.Azure.Devices.Client.Exceptions.UnauthorizedException: 'CONNECT failed: RefusedNotAuthorized'

enter image description here

I am not sure what I am missing here though I understand it is an authentication issue, and I have already changed my Hub Uri and device key as mentioned in the tutorial.

private readonly static string s_iotHubUri = "";
// This is the primary key for the device. This is in the portal. 
// Find your IoT hub in the portal > IoT devices > select your device > copy the key. 
private readonly static string s_deviceKey = "";

Solution

  • I just figured out what is making this error. I thought the device id we can use here is just a dummy one as it is not mentioned in the tutorial, but not. So, I had two option,

    1. To create a new IoT device in the IoT Hub with the device Id I used in the simulator app (test-device) and update the device id and the key in the app
    2. Use the device Id available already

    I just created a new test-device in the Hub.

    private readonly static string s_myDeviceId = "test-device";
    private readonly static string s_iotHubUri = "youriothubname.azure-devices.net";
    private readonly static string s_deviceKey = "devicekey";
    

    After doing that changes, everything was working fine as expected.

    enter image description here