【发布时间】:2018-09-07 16:15:59
【问题描述】:
我需要自动创建 Azure Batch 帐户。其中一部分是从现有 Azure 密钥保管库向帐户添加证书。我想我拥有我需要的所有部件,但我无法将它们全部组合在一起;我有一个KeyVault.Models.CertificateBundle 对象和一个Management.Batch.Models.BatchAccount 对象,但我不知道如何让一个对象进入另一个对象。
我的代码如下所示:
// Create Batch account
var storageAccount = new Models.AutoStorageBaseProperties(storageAccountId);
mgmtClient.BatchAccount.Create(resourceGroupName, accountName,
new Models.BatchAccountCreateParameters()
{
Location = clusterZone,
AutoStorage = storageAccount
});
string certName;
Models.CertificateCreateOrUpdateParameters certParams;
// Add certificate
using (KeyVaultClient kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetKeyVaultToken)))
{
var cert = kvClient.GetCertificateAsync(certId).GetAwaiter().GetResult();
string thumbprint = Convert.ToBase64String(cert.X509Thumbprint);
string cer = Convert.ToBase64String(cert.Cer);
certParams = new Models.CertificateCreateOrUpdateParameters(Convert.ToBase64String(cert.Cer), cert.Id, thumbprint: thumbprint, format: Models.CertificateFormat.Cer, type: cert.ContentType);
certName = $"SHA1-{thumbprint}"; // not sure about this one
}
// failing with a complaint about the cert name
mgmtClient.Certificate.Create(resourceGroupName, accountName, certName, certParams);
我在这段代码中遇到的确切错误是:
'certificateName' does not match expected pattern '^[\\w]+-[\\w]+$'.
certName 看起来像 SHA1-XXXXXXXXXXXXXXXXXXXXXX+XXXX=。指纹中有一些非字母数字字符。我只是猜测这是 SHA1,但除此之外,这个名字对我来说很合适。我不确定我错过了什么。
我也很乐意接受某人对这个特定问题的更简单的解决方案。
【问题讨论】:
-
根据你的报错信息,表示certName不正确。您是否尝试过使用通用名称?例如:mycert。您还可以使用 fiddler 来捕获详细信息。
-
来自官方文档:证书的标识符。这必须由用破折号分隔的算法和指纹组成,并且必须与请求中的证书数据匹配。例如 SHA1-a3d1c5。
-
是的,你是对的。证书名称应为 SHA1-a3d1c5 格式。但在你的情况下, certName 不匹配。我也在我这边测试它。更多信息请参考我的回答。
标签: c# .net azure azure-keyvault azure-batch