【问题标题】:X509CertificateCollection problems in an Azure webjobAzure webjob 中的 X509CertificateCollection 问题
【发布时间】:2016-09-03 17:10:07
【问题描述】:

我们正在开发一个需要与多台服务器通信的 Azure 网络作业,每台服务器都需要单独的 SSL 连接。

我们将证书存储在外部服务器中,并在运行时将它们与相应的 SSL 连接设置一起加载。当我们调用 X509Certificate2 构造函数以将其添加到 X509CertificateCollection 时,webjob 将停止并退出代码 -1073740940,并且其状态变为“PendingRestart”。我们的猜测是 X509Certificate2 类与 webjobs 不兼容,但我们找不到任何有关如何解决此问题的提示。

产生问题的代码行是-

新的 X509Certificate2(sslCertificateBytes, socketSettings_.CertificatePassword))

private X509CertificateCollection GetClientCertificates(byte[] sslCertificateBytes)
        {
            log_?.OnEvent($"{nameof(SSLStreamFactory)} function {nameof(GetClientCertificates)} started");
            X509CertificateCollection result = new X509Certificate2Collection();
            log_?.OnEvent($"{nameof(X509CertificateCollection)} {nameof(result)} construction successfull");
            try
            {
                if (sslCertificateBytes != null)
                {
                    log_?.OnEvent($"{nameof(sslCertificateBytes)}  enumerable != null");
                    result.Add(new X509Certificate2(sslCertificateBytes, socketSettings_.CertificatePassword));
                    log_?.OnEvent($"result.Add successful");
                }
                else if (!string.IsNullOrEmpty(socketSettings_.CertificatePath))
                {
                    log_?.OnEvent($"{nameof(socketSettings_.CertificatePath)} != null");
                    result = new X509Certificate2Collection();
                    log_?.OnEvent($"{nameof(X509CertificateCollection)} {nameof(result)} construction successfull");
                    var clientCert = StreamFactory.LoadCertificate(socketSettings_.CertificatePath, socketSettings_.CertificatePassword, log_);
                    log_?.OnEvent($"{nameof(StreamFactory.LoadCertificate)} function ended");
                    if (clientCert != null)
                    {
                        result.Add(clientCert);
                        log_?.OnEvent($"result.Add successful");
                    }
                }
            }
            catch (Exception ex)
            {
                log_?.OnEvent($"{nameof(SSLStreamFactory)} function {nameof(GetClientCertificates)} raised exception: {ex.Message}");
                throw;
            }
            log_?.OnEvent($"{nameof(SSLStreamFactory)} function {nameof(GetClientCertificates)} ended");
            return result;
        }

有没有办法在 Azure Webjobs 上管理 SSL 证书? 提前致谢

【问题讨论】:

    标签: azure ssl azure-webjobs


    【解决方案1】:

    Azure webjobs 运行在与其父 webapp 相同的环境中。您可以按照本文将证书导入 webapp :

    简而言之:

    • 将证书上传到 Azure。
    • 添加一个名为 WEBSITE_LOAD_CERTIFICATES 的应用设置,其值设置为证书的指纹(使其可供您的 Web 应用访问)

    要记住的重要事项:

    证书将安装到工作进程的 ApplicationPool Identity 的个人证书存储中。

    所以要从您的 webapp 或 webjob 访问证书:

    var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    certStore.Open(OpenFlags.ReadOnly);
    certCollection = certStore.Certificates.Find(
        X509FindType.FindByThumbprint,
        // Replace below with your cert's thumbprint
        "E661583E8FABEF4C0BEF694CBC41C28FB81CD870",
        false);
    
    // Get the first cert with the thumbprint
    if (certCollection.Count > 0)
    {
        X509Certificate2 cert = certCollection[0];
    }
    
    certStore.Close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2016-07-17
      相关资源
      最近更新 更多