2
使用由Azure資源管理器支持的Rest Management APIS,以下代碼將keyvault的證書添加到ARM。使用ARM(不適用於ssl)將PFX證書添加到Azure WebApp
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提供不涉及手動上傳到門戶的證書?
最近我有類似的問題 - 參見[如何將證書添加到Azure的RM網站使用PowerShell(http://stackoverflow.com/問題/ 34727287 /如何添加證書到天藍色rm-website-with-powershell)(雖然我正在計算您的代碼!) –
您是否從其他位置獲得該代碼? appsettings部分對我來說'感覺'錯誤(但我不確定如何/爲什麼 - 因此可能完全錯誤) –
我只是將它們拼湊在一起。無論如何,你是否成功了Azure RM網站powersell?然後我會看到他們是如何做到的 –