Azure C#KeyVaultErrorException:操作返回了无效的状态代码';禁止';
我正在编写一个程序,试图通过访问KeyVault将机密(Azure C#KeyVaultErrorException:操作返回了无效的状态代码';禁止';,c#,azure,azure-keyvault,C#,Azure,Azure Keyvault,我正在编写一个程序,试图通过访问KeyVault将机密(OneAuthzaAuthentication)访问到Azure表存储。我将遵循本教程中列出的步骤: 我创建了一个名为ITALocalBuildSecrets的密钥库: 使用以下DNS名称: 我还有另一个名为(OneAuthZAuthentication)的秘密: 我已经在active directory中创建了一个应用程序(OneAuthZUserApplication),您可以看到下面显示的应用程序(客户端)ID: 我为OneAu
OneAuthzaAuthentication
)访问到Azure表存储。我将遵循本教程中列出的步骤:
我创建了一个名为ITALocalBuildSecrets
的密钥库:
使用以下DNS名称:
我还有另一个名为(OneAuthZAuthentication
)的秘密:
我已经在active directory中创建了一个应用程序(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()}");
}
考虑到我已将
OneAuthZUserApplication
应用程序授权给密钥库,这似乎有些奇怪。您使用的权限是什么?此外,我认为您在获取令牌时缺少配置作用域的步骤
并将其提供给您的令牌请求
另外,如果这些是Azure函数,为什么不使用函数MSI来检索秘密呢 我按照您的步骤进行操作,并使用您的代码进行测试,所有这些都非常有效 请在添加
访问策略
后前往确认,记住单击保存按钮
这让我有过几次这样的经历。他们应该有UI闪存,在更改时保存按钮。
Function1. Microsoft.Azure.KeyVault: Operation returned an invalid status code 'Forbidden'.
string[] scopeArray = new string[] { "https://vault.azure.net/.default" };