gpt4 book ai didi

c# - Azure C# KeyVaultErrorException : Operation returned an invalid status code 'Forbidden'

转载 作者:行者123 更新时间:2023-12-03 05:35:14 26 4
gpt4 key购买 nike

我正在编写一个程序,尝试通过访问 KeyVault 来访问 Azure 表存储的 secret (OneAuthZAuthentication)。我正在按照本教程中列出的步骤操作:https://jeanpaul.cloud/2019/12/07/azure-key-vault-access-from-c/

我创建了一个名为 ITALocalBuildSecrets 的 Key Vault: enter image description here

使用以下 DNS 名称:https://italocalbuildsecrets.vault.azure.net/ enter image description here

我还有另一个 secret ,其名称如下 (OneAuthZAuthentication): enter image description here

我已在事件目录中创建了一个应用程序 (OneAuthZUserApplication),您可以看到下面显示的应用程序(客户端)ID: 奥 git _a我为 OneAuthZUserApplication 创建了一个客户端 key : 奥 git _a我授权控制台应用程序 (OneAuthZUserApplication) 作为访问策略: 奥 git _a并且您可以清楚地看到正在注册的访问策略: enter image description here

下面是我正在运行的代码:

    // 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 应用程序授权给 key 保管库,这似乎很奇怪。

最佳答案

我按照您的步骤并使用您的代码进行测试,一切都运行良好。

添加访问策略后请前往确认,记得点击保存按钮。

enter image description here

关于c# - Azure C# KeyVaultErrorException : Operation returned an invalid status code 'Forbidden' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63067791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com