0

我正在尝试将 ASP.NET Core 边缘模块连接到边缘运行时集线器(本地),但它没有连接并且失败并出现 CONNECT failed: RefusedNotAuthorized 异常。我有标准的 .net 核心模块,它们连接到边缘集线器并发布消息,但 ASP.NET 核心边缘模块没有。.net core 和 asp.net core edge 模块都是从 Azure IOT Edge 门户推送的。

  /// <summary>
    /// Initializes the DeviceClient and sets up the callback to receive
    /// messages containing temperature information
    /// </summary>
    static async Task Init(string connectionString, bool bypassCertVerification = false)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString() + " Connection String {0}", connectionString);

        MqttTransportSettings mqttSetting = new MqttTransportSettings(Microsoft.Azure.Devices.Client.TransportType.Mqtt_Tcp_Only);
        // During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production
        if (bypassCertVerification)
        {
            mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
        }
        ITransportSettings[] settings = { mqttSetting };

        try
        {
            // Open a connection to the Edge runtime
            DeviceClient ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings);
            await ioTHubModuleClient.OpenAsync();

            Console.WriteLine(DateTime.Now.ToLongTimeString() + " IoT Hub module client initialized.");
        }
        catch(Exception ex)
        {
            Console.WriteLine(DateTime.Now.ToLongTimeString() + ex.Message);
        }
    }
4

2 回答 2

0

我已经用你提供的代码测试了这个问题,它有效。我认为你需要检查设备的连接字符串。如果您使用错误的连接字符串连接设备客户端,CONNECT failed: RefusedNotAuthorized则会发生错误。您可以从 Azure 门户复制连接字符串(IoT Edge->->连接字符串主键)。

于 2018-02-02T06:32:31.143 回答
0

此时一个模块需要两个授权。一个是连接字符串,它将使模块能够连接到 IoTHub,但是我们在 edgeHub 上有一个服务器证书来建立到 edgeHub 的连接。该证书通过文件系统和建立文件路径的环境变量传递给模块。

你的模块中是否有“InstallCert()”函数,它被调用了吗?

        static void InstallCert()
    {
        string certPath = Environment.GetEnvironmentVariable("EdgeModuleCACertificateFile");
        if (string.IsNullOrWhiteSpace(certPath))
        {
            // We cannot proceed further without a proper cert file
            Console.WriteLine($"Missing path to certificate collection file: {certPath}");
            throw new InvalidOperationException("Missing path to certificate file.");
        }
        else if (!File.Exists(certPath))
        {
            // We cannot proceed further without a proper cert file
            Console.WriteLine($"Missing path to certificate collection file: {certPath}");
            throw new InvalidOperationException("Missing certificate file.");
        }
        X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadWrite);
        store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(certPath)));
        Console.WriteLine("Added Cert: " + certPath);
        store.Close();
    }
于 2018-02-03T02:00:34.117 回答