如何通过本地asp.net应用程序在Azure Key Vault中设置机密

时间:2020-04-17 22:24:37

标签: azure azure-web-app-service azure-keyvault

我有一个本地asp.net core 3.1应用程序,我想在Azure Key Vault中设置一个秘密。以下是我从Microsoft使用的代码:

string secretName = "xxSecret";

string keyVaultName = Environment.GetEnvironmentVariable("KEY_VAULT_NAME");
var kvUri = "https://" + keyVaultName + ".vault.azure.net";
var secretClient = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());

string secretValue = "test";
secretClient.SetSecret(secretName, secretValue);
KeyVaultSecret secret = secretClient.GetSecret(secretName);

当我尝试设置机密时,在邮递员中出现以下错误:

Azure.Identity.AuthenticationFailedException: DefaultAzureCredential authentication failed.
 ---> Azure.Identity.AuthenticationFailedException: SharedTokenCacheCredential authentication failed.
 ---> Microsoft.Identity.Client.MsalServiceException: AADSTS70002: The client does not exist or is not 
enabled for consumers. If you are the application developer, configure a new application through the 
App Registrations in the Azure Portal

我不想注册此应用,但因为要在本地调试此应用。我猜问题是我没有正确设置访问策略。如何授予我的本地应用访问权限?

(在本地运行应用程序之前,我使用Azure PowerShell身份验证到我的Azure目录。)

1 个答案:

答案 0 :(得分:3)

如何授予我的本地应用访问权限?

对于本地开发,AzureServiceTokenProvider使用Visual Studio,Azure CLI或Azure AD集成身份验证来获取令牌。每个选项都按顺序尝试,并且库使用成功的第一个选项。

要使用Visual Studio进行身份验证,请执行以下操作:

登录Visual Studio并使用_layout/results.html>选项打开Tools

选择Options,选择一个本地开发帐户,然后选择Azure Service Authentication

天蓝色时,您需要转到Azure密钥库。点击OK并添加您的帐户,该帐户的登录名和密码之前为获取并设置权限,以供秘密使用。 然后,您可以使用代码来获取秘密值。

此外,您可以使用Access Policies来获取机密,而无需初始化您的机密值。

AzureServiceTokenProvider