X59Certificate2在Azure Webjobs中调用Google API失败

Per*_*los 6 azure google-bigquery azure-webjobs

我有一个使用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));
Run Code Online (Sandbox Code Playgroud)

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

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

Per*_*los 12

使用X509KeyStorageFlags.Exportable在IIS中不可用.我在自己的虚拟机上尝试过Azure Webjobs,Azure网站和IIS.它在使用IISExpress时在开发环境中工作,因为该进程在用户的上下文中运行.

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

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

var certificate = new X509Certificate2(data, "notasecret", X509KeyStorageFlags.Exportable); var xml = certificate.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
 });
Run Code Online (Sandbox Code Playgroud)

希望这有助于其他人.


小智 5

此解决方案在Azure设置中有效.但是我也发现了一个更简单的需求.只需添加额外的旗帜,这样每个人都很开心......

 var certificate = new X509Certificate2(data,"notasecret",
                       X509KeyStorageFlags.MachineKeySet | 
                       X509KeyStorageFlags.PersistKeySet | 
                       X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)

我找到了这个解决方案


归档时间:

查看次数:

1088 次

最近记录:

9 年,2 月 前