【问题标题】:Asp.net core module does not connect to Azure IOT Edge HubAsp.net 核心模块未连接到 Azure IOT Edge Hub
【发布时间】:2018-02-01 17:09:35
【问题描述】:

我正在尝试将 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);
        }
    }

【问题讨论】:

    标签: asp.net-core-2.0 azure-iot-edge


    【解决方案1】:

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

    【讨论】:

    • 您是否在 ASP.NET Core 模块中对此进行了测试?请记住,ConnectionString 是由 Edge 运行时在环境变量中提供的,我没有对其进行硬编码
    • 如果是 ASP.NET Core 模块,能否提供 github 中的示例项目?
    • 问题在于未使用 Microsoft.Azure.Device.Client 框架的预览版。添加 thorugh Nuget 包管理器时,必须选中复选框以加载预览版本
    【解决方案2】:

    此时一个模块需要两个授权。一个是连接字符串,它将使模块能够连接到 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();
        }
    

    【讨论】:

    • 我有这个方法,但正如默认模板中的注释所说,它没有在 Windows 中安装证书。我添加了日志消息,即使在我的其他 .net 核心模块中也没有调用它,它仍然连接。(我相信证书用于验证 hte TLS 连接,我们现在将验证硬编码为 true)
    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多