【发布时间】:2019-06-03 23:53:33
【问题描述】:
我有一个工作进程作为 Azure 中的经典云服务运行。此过程需要从 Azure 密钥保管库中检索一个值,并且我需要将保管库身份验证机密保存在我的源代码树之外。
托管身份似乎不适用于经典云工作者,因此我正在查看证书。我已经通过证书进行身份验证,可以在本地工作,方法是创建一个证书,然后在 Azure Active Directory 中上传以进行我的应用注册:
证书创建:
New-SelfSignedCertificate -Subject "CN=MyFineCertificate" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature
使用它连接到密钥保管库的代码(在本地工作):
private static KeyVaultClient GetClient()
{
var certificate = GetCertificate();
var assertion = new ClientAssertionCertificate(clientId, certificate);
var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback((a, r, s) => GetAccessTokenUsingCert(a, r, s, assertion)));
return client;
}
private static X509Certificate2 GetCertificate()
{
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
var results = certStore.Certificates.Find(/* criteria */);
return results[0];
}
private static async Task<string> GetAccessToken(string authority, string resource, string scope, ClientAssertionCertificate cert)
{
var authContext = new AuthenticationContext(authority, TokenCache.DefaultShared);
var result = await authContext.AcquireTokenAsync(resource, cert);
return result.AccessToken;
}
到目前为止一切顺利。但是,我希望我的 Azure 云工作者能够执行此操作,因此我需要在那里提供我的证书。我的天真假设是,我可以从门户中我的云工作者的“证书”面板上传证书 (pfx)。
很遗憾,我的云工作者无法找到它。如果我在 Azure 上运行它,上传我的证书后,它不会显示:
foreach (StoreName name in Enum.GetValues(typeof(StoreName)))
{
foreach (StoreLocation location in Enum.GetValues(typeof(StoreLocation)))
{
var certStore = new X509Store(name, location);
certStore.Open(OpenFlags.ReadOnly);
foreach (var res in certStore.Certificates)
{
/* log certificate */
}
}
}
为什么不显示?我是否走在正确的轨道上,还是我完全误解了它的工作原理?
【问题讨论】:
标签: c# azure azure-cloud-services x509certificate2 azure-keyvault