gpt4 book ai didi

azure - X509Certificate2 在调用 Google API 的 Azure Webjobs 中失败

转载 作者:行者123 更新时间:2023-12-04 13:16:26 26 4
gpt4 key购买 nike

我有一个使用 Azure WebJobs 计划的控制台应用程序。尝试读取p12证书的私钥时执行总是失败。有趣的是,我无法捕获异常,我不得不使用旧的 Console.WriteLine 来调试。

这是我的代码片段:

var certificate = new X509Certificate2(data, "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { BigqueryService.Scope.Bigquery }
}.FromCertificate(certificate));

其他帖子提到标志应该是 X509KeyStorageFlags.MachineKeySet 但不幸的是,这会导致 Google API 出现错误。它需要设置 X509KeyStorageFlags.Exportable 标志。

任何人都可以确认 X509KeyStorageFlags.Exportable 可在 Azure 网站和 WebJobs 上使用吗?

最佳答案

在 IIS 中无法使用 X509KeyStorageFlags.Exportable。我已经在自己的虚拟机上尝试使用 Azure Webjobs、Azure Websites 和 IIS。当使用 IISExpress 时,它可以在开发环境中工作,因为该进程在用户上下文中运行。

因此,为了使其在 IIS 上下文(包括 Webjobs)中工作,必须将其设置为 MachineKeySet,但 Google API 将失败,因为它需要私钥。

我的问题的解决方案实际上非常简单,创建一个控制台应用程序,该应用程序创 build 置了 Exportable 标志的 X509Certificate2 对象,然后调用ToXmlString()。这是片段:

var 证书 = new X509Certificate2(data, "notasecret", X509KeyStorageFlags.Exportable);
var xml = 证书.PrivateKey.ToXmlString(true);

然后,我保存 XML 并使用该 XML 创建一个 RSACryptoServiceProvider,如下所示:

var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlKey);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { BigqueryService.Scope.Bigquery },
Key = rsa
});

希望这对其他人有帮助。

关于azure - X509Certificate2 在调用 Google API 的 Azure Webjobs 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25874317/

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