首页 > 解决方案 > 本地 azure 服务结构上的证书空错误

问题描述

尝试在我的本地运行 Azure Service Fabric 应用程序,所有服务都在运行,除了一个抛出证书不能为空异常的服务。下面是获取证书的代码片段。

已在本地计算机和当前用户的本地安装证书。

在此处输入图像描述

在此处输入图像描述

/// <summary>
/// Finds the ASP .NET Core HTTPS development certificate in development environment. Update this method to use the appropriate certificate for production environment.
/// </summary>
/// <returns>Returns the ASP .NET Core HTTPS development certificate</returns>
private static X509Certificate2 GetCertificateFromStore()
{
    string aspNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    if (string.Equals(aspNetCoreEnvironment, "Development", StringComparison.OrdinalIgnoreCase))
    {
        const string aspNetHttpsOid = "1.3.6.1.4.1.311.84.1.1";
        const string CNName = "CN=localhost";
        using (X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
        {
            store.Open(OpenFlags.ReadOnly);
            var certCollection = store.Certificates;
            var currentCerts = certCollection.Find(X509FindType.FindByExtension, aspNetHttpsOid, true);
            currentCerts = currentCerts.Find(X509FindType.FindByIssuerDistinguishedName, CNName, true);
            return currentCerts.Count == 0 ? null : currentCerts[0];
        }
    }
    else
    {
        throw new NotImplementedException("GetCertificateFromStore should be updated to retrieve the certificate for non Development environment");
    }
}

标签: .netazureasp.net-coreazure-service-fabricx509certificate

解决方案


您应该尝试将证书文件复制到 Service Fabric 服务帐户可以在启动时获取它们的位置,然后直接读取它们,或者将它们写入以**new X509Store(StoreName.My, StoreLocation.CurrentUser)**供后续使用。

检查此文档以获取更多参考:

https://github.com/dotnet/corefx/blob/master/Documentation/architecture/cross-platform-cryptography.md#x509store

请确保您没有遵循上述场景之一。

您可以使用以[SetupEntryPoint][1]用户**AccountType="LocalSystem"**身份运行的 a 来运行SetupEntryPoint

或者,您可以使用 Azure 密钥保管库来存储证书,然后从那里读取它。您可以在此处找到示例代码:

https://docs.microsoft.com/en-us/azure/service-fabric/how-to-managed-identity-service-fabric-app-code#accessing-key-vault-from-a-service-fabric-应用程序使用托管标识

希望能帮助到你。


推荐阅读