gpt4 book ai didi

.net - 本地 azure 服务结构上的证书为空错误

转载 作者:行者123 更新时间:2023-12-03 04:06:32 28 4
gpt4 key购买 nike

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

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

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");
}
}

最佳答案

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

查看此文档以获取更多引用:

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

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

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

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

https://learn.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

希望有帮助。

关于.net - 本地 azure 服务结构上的证书为空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606650/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com