【发布时间】:2017-11-19 21:36:14
【问题描述】:
我已创建 Key Vault 并输入了一个机密。当我使用 .NET 在本地运行我的服务时,我可以通过密钥库检索秘密。这是我所做的: 1) 创建 SSL 证书 2) 使用该 SSL 证书创建 AD 应用程序 3)为上述应用程序创建了一个服务原则 4) 授予对此应用程序的完整密钥库访问权限 5) 我将 VaultURI、ServicePrincipal.Application ID 和证书指纹放入 Web.config 文件中 6) 我还将该证书的 *.pfx 上传到我的云服务
当我在本地运行我的服务时,我能够检索到密钥。我什至尝试通过 powershell 检索秘密,我已经成功了。当我将代码部署到 Azure 时,我无法检索密钥。
上面写着:
Type : Microsoft.Azure.KeyVault.Models.KeyVaultErrorException, Microsoft.Azure.KeyVault, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Message : Secret not found: QSAccounts7126 Source : Microsoft.Azure.KeyVault Help link :
我花了 3 天时间查看它并重新测试所有可能的情况,但还没有弄清楚哪里出了问题。有人可以帮助确定问题或指导我正确的调试路径吗? 我什至尝试在 Azure 中以调试模式发布云服务,但由于某种原因,这也不起作用。
您能提供的任何帮助将不胜感激。
private async Task<string> getSecretConnection(string connectionName)
{
var kvName = ConfigurationManager.AppSettings["vaultName"];
var kvClientId = ConfigurationManager.AppSettings["clientId"];
var kvClientThumbprint = ConfigurationManager.AppSettings["clientThumbprint"];
using (keyVaultHelper = new AzureKeyVaultHelper(kvClientId, kvClientThumbprint, kvName))
{
var bundle = await keyVaultHelper.GetAzureKeyVaultSecretAsync(connectionName);
return bundle;
}
public async Task<string> GetAzureKeyVaultSecretAsync(string secretName)
{
var bundle = await this.KvClient.GetSecretAsync(KeyVaultUrl, secretName);
return bundle.Value;
}
这是运行身份验证的代码:
private async Task<string> getAccessTokenFromSPNAsync(string authority, string resource, string scope)
{
//clientID and clientSecret are obtained by registering
//the application in Azure AD
var certificate = CertificateHelper.FindCertificateByThumbprint(this.ClientThumbprint);
var assertionCert = new ClientAssertionCertificate(this.ClientId, certificate); //needed for authentication
var clientCredential = new ClientCredential(this.ClientId, this.ClientThumbprint);
var authContext = new AuthenticationContext(authority, TokenCache.DefaultShared);
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, assertionCert);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the token from Azure AD using certificate");
}
return result.AccessToken;
}
【问题讨论】:
-
您能否发布一些代码,以便我们了解您是如何尝试访问秘密值的?
-
我刚刚在上面发布了我的代码。如果您还需要什么,请告诉我:)
-
您是否尝试在 Azure WebApp 应用设置中添加
WEBSITE_LOAD_CERTIFICATES等于指纹值。在这种情况下,您的证书将被加载到您的 Web 应用程序个人证书存储中。更多细节可以参考这个blog。
标签: powershell azure ssl azure-web-app-service azure-keyvault