Azure C# KeyVaultErrorException:操作返回无效状态代码 'Forbidden'
Azure C# KeyVaultErrorException: Operation returned an invalid status code 'Forbidden'
我正在编写一个程序,试图通过访问 KeyVault 来访问 Azure Table 存储的机密 (OneAuthZAuthentication
)。我正在按照本教程中列出的步骤进行操作:https://jeanpaul.cloud/2019/12/07/azure-key-vault-access-from-c/
我创建了一个名为 ITALocalBuildSecrets
的 Key Vault:
使用以下 DNS 名称:https://italocalbuildsecrets.vault.azure.net/
我还有另一个 秘密,名字如下 (OneAuthZAuthentication
):
我在活动目录(OneAuthZUserApplication
)中创建了一个应用程序,您可以看到下面显示的应用程序(客户端)ID:
我为 OneAuthZUserApplication
创建了一个客户端密钥:
我授权控制台应用程序 (OneAuthZUserApplication
) 作为访问策略:
您可以清楚地看到正在注册的访问策略:
下面是我的代码 运行:
// Retrieves the access token necessary to gain authentication into the key vault
[FunctionName("GetToken")]
public static async System.Threading.Tasks.Task<string> GetToken(string authority, string resource, string scope)
{
var clientId = "5cf497b0-3467-456a-a03a-4d4414b*****"; // Stars are for security reasons :D
var clientSecret = "468.26i5Wc.nQ6TYL-eOvBmcto.t.*****"; // Stars are for security reasons
ClientCredential credential = new ClientCredential(clientId, clientSecret);
var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
var result = await context.AcquireTokenAsync(resource, credential);
return result.AccessToken;
}
// Retrieves the access key vault accountKey (needed to authenticate access into the role assignments table)
public static string GetVaultValue()
{
KeyVaultClient client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
var vaultAddress = "https://italocalbuildsecrets.vault.azure.net/";
var secretName = "OneAuthZAuthentication";
var secret = client.GetSecretAsync(vaultAddress, secretName).GetAwaiter().GetResult();
return secret.Value;
}
[FunctionName("Function1")]
// Function that reads a small portion of the role assignments table (OneAuthZRoleAssignments) every
// configurable number of times
public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("%TimerTriggerPeriod%")]TimerInfo myTimer, ILogger log)
{
Console.WriteLine($"Secret Value from Vault is: {GetVaultValue()}");
}
我收到以下错误:
Function1. Microsoft.Azure.KeyVault: Operation returned an invalid status code 'Forbidden'.
考虑到我已将 OneAuthZUserApplication
应用程序授权到密钥库,这看起来确实很奇怪。
您使用的是什么权限?此外,我认为您在获取令牌时缺少配置范围的步骤。 .
string[] scopeArray = new string[] { "https://vault.azure.net/.default" };
并将其提供给您的令牌请求。
此外,如果这些是 Azure Functions,为什么不使用 MSI 函数来检索机密? See here
我按照你的步骤,用你的代码进行了测试,一切都很好。
添加后请去确认Access policy
,记得点击保存按钮。
我正在编写一个程序,试图通过访问 KeyVault 来访问 Azure Table 存储的机密 (OneAuthZAuthentication
)。我正在按照本教程中列出的步骤进行操作:https://jeanpaul.cloud/2019/12/07/azure-key-vault-access-from-c/
我创建了一个名为 ITALocalBuildSecrets
的 Key Vault:
使用以下 DNS 名称:https://italocalbuildsecrets.vault.azure.net/
我还有另一个 秘密,名字如下 (OneAuthZAuthentication
):
我在活动目录(OneAuthZUserApplication
)中创建了一个应用程序,您可以看到下面显示的应用程序(客户端)ID:
OneAuthZUserApplication
创建了一个客户端密钥:
OneAuthZUserApplication
) 作为访问策略:
下面是我的代码 运行:
// Retrieves the access token necessary to gain authentication into the key vault
[FunctionName("GetToken")]
public static async System.Threading.Tasks.Task<string> GetToken(string authority, string resource, string scope)
{
var clientId = "5cf497b0-3467-456a-a03a-4d4414b*****"; // Stars are for security reasons :D
var clientSecret = "468.26i5Wc.nQ6TYL-eOvBmcto.t.*****"; // Stars are for security reasons
ClientCredential credential = new ClientCredential(clientId, clientSecret);
var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
var result = await context.AcquireTokenAsync(resource, credential);
return result.AccessToken;
}
// Retrieves the access key vault accountKey (needed to authenticate access into the role assignments table)
public static string GetVaultValue()
{
KeyVaultClient client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
var vaultAddress = "https://italocalbuildsecrets.vault.azure.net/";
var secretName = "OneAuthZAuthentication";
var secret = client.GetSecretAsync(vaultAddress, secretName).GetAwaiter().GetResult();
return secret.Value;
}
[FunctionName("Function1")]
// Function that reads a small portion of the role assignments table (OneAuthZRoleAssignments) every
// configurable number of times
public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("%TimerTriggerPeriod%")]TimerInfo myTimer, ILogger log)
{
Console.WriteLine($"Secret Value from Vault is: {GetVaultValue()}");
}
我收到以下错误:
Function1. Microsoft.Azure.KeyVault: Operation returned an invalid status code 'Forbidden'.
考虑到我已将 OneAuthZUserApplication
应用程序授权到密钥库,这看起来确实很奇怪。
您使用的是什么权限?此外,我认为您在获取令牌时缺少配置范围的步骤。
string[] scopeArray = new string[] { "https://vault.azure.net/.default" };
并将其提供给您的令牌请求。
此外,如果这些是 Azure Functions,为什么不使用 MSI 函数来检索机密? See here
我按照你的步骤,用你的代码进行了测试,一切都很好。
添加后请去确认Access policy
,记得点击保存按钮。