使用c#:“ import .pfx证书”使用c#:“证书内容”无法读取。

发布于 2025-01-31 22:32:08 字数 861 浏览 4 评论 0原文

我一直在尝试将证书进出来,将证书导入Azure的KeyVault服务,但没有成功。该证书已成功地通过Azure Portal正确导入,我什至可以通过X509Certificate2加载它,并且似乎运行良好。

事实是,一旦我执行importcertificateasync方法,它就会引发一个例外:

无法读取指定的PKCS#12 X.509证书内容。 请检查证书是否有效PKCS#12格式。状态:400 (不良要求)

实际上,我实际上是从本地存储中加载证书的file.readallBytes,创建importCertificateOptions 和调用import> importcertificatificateasync方法>离开。

var certificate = new X509Certificate2(data, password);
if (!certificate.HasPrivateKey)
   return;

var client = new CertificateClient(new Uri("https://myenvironment-dev.vault.azure.net/"), new DefaultAzureCredential());

var importOptions = new ImportCertificateOptions("importedFromCode", data);
var result = await client.ImportCertificateAsync(importOptions, cancellationToken);

我在做什么错?

I have been trying back and forth importing a certificate into Azure's KeyVault service with no success. The certificate has been correctly imported through Azure Portal successfully and I can even load it through X509Certificate2 and seems to work well.

Thing is that once I execute the ImportCertificateAsync method it throws an exception saying:

The specified PKCS#12 X.509 certificate content can not be read.
Please check if certificate is in valid PKCS#12 format. Status: 400
(Bad Request)

I'm actually loading the certificate from a local storage with a File.ReadAllBytes, creating an ImportCertificateOptions and calling ImportCertificateAsync method right away.

var certificate = new X509Certificate2(data, password);
if (!certificate.HasPrivateKey)
   return;

var client = new CertificateClient(new Uri("https://myenvironment-dev.vault.azure.net/"), new DefaultAzureCredential());

var importOptions = new ImportCertificateOptions("importedFromCode", data);
var result = await client.ImportCertificateAsync(importOptions, cancellationToken);

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-02-07 22:32:08

傻我,我实际上不是设置证书的密码

var certificate = new X509Certificate2(data, password);
if (!certificate.HasPrivateKey)
   return;

var client = new CertificateClient(new Uri("https://myenvironment-dev.vault.azure.net/"), new DefaultAzureCredential());

var importOptions = new ImportCertificateOptions("importedFromCode", data) { Password = password };
var result = await client.ImportCertificateAsync(importOptions, cancellationToken);

Silly me I wasn't actually setting the certificate's password!

var certificate = new X509Certificate2(data, password);
if (!certificate.HasPrivateKey)
   return;

var client = new CertificateClient(new Uri("https://myenvironment-dev.vault.azure.net/"), new DefaultAzureCredential());

var importOptions = new ImportCertificateOptions("importedFromCode", data) { Password = password };
var result = await client.ImportCertificateAsync(importOptions, cancellationToken);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文