本地天蓝色服务结构上的证书空错误

问题描述 投票:1回答:1

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

在本地计算机上为本地计算机和当前用户安装了证书。

enter image description here

enter image description here

/// <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");
    }
}
.net azure asp.net-core azure-service-fabric x509certificate
1个回答
0
投票

[您应尝试将证书文件复制到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"**来运行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-application-using-managed-identity

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.