【问题标题】:Certificate null error on local azure service fabric本地 azure 服务结构上的证书空错误
【发布时间】:2020-04-23 16:12:03
【问题描述】:

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

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

/// <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】:

    您应该尝试将证书文件复制到 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

    希望对你有帮助。

    【讨论】:

    • 感谢您的输入,我已经更新了问题并添加了更多细节,但仍然遇到相同的异常
    猜你喜欢
    • 2016-08-23
    • 2018-09-21
    • 2019-01-31
    • 2012-12-11
    • 1970-01-01
    • 2022-01-21
    • 2017-03-29
    • 2023-03-14
    • 2014-04-10
    相关资源
    最近更新 更多