【发布时间】:2016-04-26 02:04:02
【问题描述】:
使用 Azure 资源管理器支持的 Rest Management APIS,以下代码将证书从 keyvault 添加到 ARM。
var secret = keyvaultClient.GetSecretAsync(vaultUri, options.CertificateName).GetAwaiter().GetResult();
var certUploaded = client.Certificates.CreateOrUpdateCertificateWithHttpMessagesAsync(
options.ResourceGroupName, options.CertificateName,
new Certificate {
PfxBlob = secret.Value,
Location = app.Body.Location
}).GetAwaiter().GetResult();
var appSettings = client.Sites.ListSiteAppSettingsWithHttpMessagesAsync(options.ResourceGroupName, options.WebAppName).GetAwaiter().GetResult();
var existing = (appSettings.Body.Properties["WEBSITE_LOAD_CERTIFICATES"] ?? "").Split(',').ToList();
if (!existing.Contains(certUploaded.Body.Thumbprint))
existing.Add(certUploaded.Body.Thumbprint);
appSettings.Body.Properties["WEBSITE_LOAD_CERTIFICATES"] = string.Join(",",existing);
appSettings.Body.Properties[$"CN_{options.CertificateName}"] = certUploaded.Body.Thumbprint;
var result = client.Sites.UpdateSiteAppSettingsWithHttpMessagesAsync(options.ResourceGroupName, options.WebAppName, appSettings.Body).GetAwaiter().GetResult();
问题是在webapp中加载它时
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
X509FindType.FindByThumbprint,
// Replace below with your cert's thumbprint
"0CE28C6246317AEB00B88C88934700865C71CBE0",
false);
Trace.TraceError($"{certCollection.Count}");
Console.WriteLine($"{certCollection.Count}");
// Get the first cert with the thumbprint
if (certCollection.Count > 0)
{
X509Certificate2 cert = certCollection[0];
// Use certificate
Console.WriteLine(cert.FriendlyName);
}
certStore.Close();
它没有加载。
我还注意到,在门户中上传的证书在 ARM 中不存在,只有在帖子开头添加了代码的证书存在。:
那么我们需要做些什么来使证书可用于不涉及手动上传到门户的 webapp?
【问题讨论】:
-
我最近遇到了类似的问题 - 请参阅 How to add a certificate to an Azure RM website with Powershell(我正在计算您的代码!)
-
您是从其他地方获得的代码吗? appsettings 部分对我来说“感觉”不对(但我不确定如何/为什么 - 所以很可能完全错了)
-
我只是一起破解了它。无论如何,您成功使用 Azure RM 网站 powersell 了吗?那我就看看他们是怎么做到的
-
我可以看到它与添加 SSL 有关。我没有添加 SSL。我只需要应用程序的证书来解密一些值。
-
我的答案主要来自资源资源管理器、资源模板和 Azure Powershell 的来源。如果您从资源资源管理器中配置的解决方案的外观开始,然后尝试使用模板的 sn-p 将其推送。我看不出添加 SSL 证书和普通证书之间存在巨大差异 - 但是我在上面写的答案非常有效。
标签: azure certificate azure-web-app-service