c# - 将证书导入 Azure 密钥库:操作返回了无效的状态代码“冲突”
问题描述
我正在尝试使用以下代码将 .PFX 文件(首先转换为 base64 文件)导入 Azure Keyvault。
但是我收到错误:操作返回了无效的状态代码“冲突”
Azure KeyVault 上绝对没有其他证书。
public async Task ImportCertificate(string base64FileCertFile, string CertPasswordText, string name)
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
Message = "Your application description page.";
int retries = 0;
//byte[] fileData = null;
//using (var binaryReader = new BinaryReader(request.Files[0].InputStream))
//{
// fileData = binaryReader.ReadBytes(request.Files[0].ContentLength);
//}
//var base64EncodedCertificate = Convert.ToBase64String(fileData);
bool retry = false;
try
{
/* The below do while logic is to handle throttling errors thrown by Azure Key Vault. It shows how to do exponential backoff which is the recommended client side throttling*/
do
{
long waitTime = Math.Min(GetWaitTime(retries), 2000000);
var result = await keyVaultClient.ImportCertificateAsync(ConfigurationManager.AppSettings["VaultUrl"].ToString(), name, base64FileCertFile, CertPasswordText);
Message = result.Id;
retry = false;
}
while (retry && (retries++ < 10));
}
/// <exception cref="KeyVaultErrorException">
/// Thrown when the operation returned an invalid status code
/// </exception>
catch (KeyVaultErrorException keyVaultException)
{
Message = keyVaultException.Message;
if ((int)keyVaultException.Response.StatusCode == 429)
retry = true;
}
}
解决方案
关于这个问题,根据我的研究,当第一次创建 KV 证书时,也会创建一个与证书同名的可寻址密钥和秘密。如果该名称已在使用中,则操作将失败并返回 http 状态代码 409(冲突)。更多详细信息,请参阅文档。所以我建议你改名字。
推荐阅读
- sql - 在 SQL 中转换和搜索图像文件的字符限制
- jquery - jQuery.trim() 不工作
- javascript - 是否可以将人类用户和机器人(蜘蛛、爬虫)分开并仅在 AMP 页面上为人类用户显示广告横幅?
- shopify - 首次亮相 shopify 主题:在收藏页面上更改图像大小
- xamarin - Xamarin Forms - 将构造函数添加到 MasterDetailPage.Master
- shopify - Shopify cart.js(铆钉版本)一次删除多个产品而不是一个
- java - Android GraphView 和 Mediaplayer 不同步
- google-app-engine - Google App Engine 高成本
- ruby-on-rails - 当我尝试在 Rails 5 中添加占位符关联记录时,嵌套字段表单出现错误
- android - 在自己的应用程序中显示网站是否合法?