如何在 Azure IOT Hub 中调试“CONNECT failed: RefusedNotAuthorized”

问题描述

我在尝试使用指纹授权连接到 Azure IOT 中心时收到详细错误“CONNECT 失败:RefusedNotAuthorized”。创建具有对称密钥授权的设备时,我可以毫无问题地进行连接。我整个周末都在各种谷歌搜索中苦苦挣扎,对如何进行调试一无所知。

我使用以下代码成功向我的 Azure IOT 中心注册了一个设备:

        ...

        var certificate = certificateHelper.CreateSelfSignedCertificate(userRequest.DeviceID.ToString());

        // connect to iot hub
        var registryManager = RegistryManager.CreateFromConnectionString("[My Connection String]");

        // define device
        Device iotDevice = new Device(userRequest.DeviceID.ToString());

        iotDevice.Authentication = new AuthenticationMechanism()
        {
            Type = AuthenticationType.SelfSigned,X509Thumbprint = new X509Thumbprint()
            {
                PrimaryThumbprint = certificate.Thumbprint,SecondaryThumbprint = certificate.Thumbprint
            }
        };

        // register
        try
        {
            iotDevice = await registryManager.AddDeviceAsync(iotDevice);
        }
        catch (DeviceAlreadyExistsException)
        {

        ...

我正在使用以下代码创建自签名证书:

    public X509Certificate2 CreateSelfSignedCertificate(string subjectName)
    {
        var ecdsa = ECDsa.Create(); // generate asymmetric key pair
        var req = new CertificateRequest("CN=" + subjectName,ecdsa,HashAlgorithmName.SHA256);
        return req.CreateSelfSigned(DateTimeOffset.Now,DateTimeOffset.Now.AddYears(1));
    }

最后,我尝试通过以下代码连接到 IOT 集线器:

        X509Certificate2 cert = new X509Certificate2(Convert.FromBase64String(device.Certificate));

        var deviceAuthentication = new DeviceAuthenticationWithX509Certificate(device.TestDeviceID.ToString(),cert);

        IotHub = DeviceClient.Create(_hostname,deviceAuthentication,TransportType.Mqtt);

        IotHub.OpenAsync().Wait();

如果有一些简单的东西是不正确的,我很想知道。但我真正感兴趣的是我如何调试它。我假设有 IOT 服务器的日志,它将为我提供有关为什么它认为该设备未经授权的更多信息。他们在哪?我是为他们查询集线器还是在门户中设置一些东西?我整个周末都在为一个一般性错误而烦恼,虽然我对证书和集线器本身有了更多了解,但我仍然遇到错误。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...