首页 > 解决方案 > 将证书导入 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;
            }
        }

标签: c#asp.net.netazureazure-keyvault

解决方案


关于这个问题,根据我的研究,当第一次创建 KV 证书时,也会创建一个与证书同名的可寻址密钥和秘密。如果该名称已在使用中,则操作将失败并返回 http 状态代码 409(冲突)。更多详细信息,请参阅文档。所以我建议你改名字。


推荐阅读