Edge Module Direct Method setup - Returns RefusedNotAuthorized

Jeffrey K 171 Reputation points
2022-12-01T21:22:52.913+00:00

We are getting an error at our custom edge module when trying to setup Directmethod. We followed the article followed in
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-device-management-get-started

We have also opened the port for 8883. Any idea what we are missing here?

Starting up
Connecting to Hub
Application start-up failed: System.AggregateException: One or more errors occurred. (CONNECT failed: RefusedNotAuthorized)
---> Microsoft.Azure.Devices.Client.Exceptions.UnauthorizedException: CONNECT failed: RefusedNotAuthorized
at Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.OpenInternalAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.OpenAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.ProtocolRoutingDelegatingHandler.OpenAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<>c__DisplayClass27_0.<<ExecuteWithErrorHandlingAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.ExecuteWithErrorHandlingAsyncT
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.OpenInternalAsync(Boolean withRetry, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.EnsureOpenedAsync(Boolean withRetry, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.<>c__DisplayClass24_0.<<EnableMethodsAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Azure.Devices.Client.TransientFaultHandling.RetryPolicy.<>c__DisplayClass34_0.<<RunWithRetryAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Azure.Devices.Client.TransientFaultHandling.RetryPolicy.RunWithRetryAsyncT
at Microsoft.Azure.Devices.Client.TransientFaultHandling.RetryPolicy.RunWithRetryAsync(Func1 taskFunc, ShouldRetry shouldRetry, Func2 isTransient, Action`3 onRetrying, Boolean fastFirstRetry, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.EnableMethodsAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.InternalClient.HandleMethodEnableAsync(CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.InternalClient.SetMethodHandlerAsync(String methodName, MethodCallback methodHandler, Object userContext, CancellationToken cancellationToken)
at Microsoft.Azure.Devices.Client.InternalClient.SetMethodHandlerAsync(String methodName, MethodCallback methodHandler, Object userContext)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at Program.Main(String[] args) in /src/Omnicell.Cloud.DataRestore.API/Program.cs:line 39

Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 36,951 Reputation points MVP Volunteer Moderator
    2022-12-12T00:27:28.067+00:00

    Hello @Jeffrey K ,

    You try to create a device client based on the C# Azure IoT Device SDK.

    Please check out this blog post with a similar approach.

    Most importantly, you need to use the correct connection string.

    This is the connection string unique for a single device:

    HostName=someuniqueiothubname.azure-devices.net;DeviceId=testdevice;SharedAccessKey=XYZ  
    

    Do not use IoT Hub connection string, looking like this:

    HostName=otheriothubname.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=ABC // DO NOT USE THIS!!!  
    

    This second connection string must never leave the save cloud environment. It this connection string is exposed outside the cloud the user can start manipulating your IoT Hub (seriously, keep it safe)!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.