c# - Azure C# KeyVaultErrorException:操作返回了无效的状态代码“禁止”
问题描述
我正在编写一个程序,试图OneAuthZAuthentication
通过访问 KeyVault 来访问 Azure 表存储的机密 ()。我正在遵循本教程中列出的步骤:https ://jeanpaul.cloud/2019/12/07/azure-key-vault-access-from-c/
使用以下DNS 名称:https ://italocalbuildsecrets.vault.azure.net/
我还有另一个名为 ( ) 的秘密OneAuthZAuthentication
:
我在活动目录中创建
了
一个应用
OneAuthZUserApplication
程序
(正在注册的访问策略:
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
应用程序授权给密钥保管库,这看起来确实很奇怪。
解决方案
推荐阅读
- javascript - React:如果组件不相关,如何调用模式
- php - SQLSTATE [42000]:语法错误或访问冲突:1066 Not unique table/alias in laravel
- javascript - Firestore 的 Firebase Cloud Functions 未触发
- php - 带有 gtag_report_conversion 的 php mysql 表单
- node.js - RabbitMQ 一个消费者/一个发布者模式
- react-native - TabNavigator 不切换选项卡。navigation.navigate 不是函数
- java - 如何在使用 Gradle 时禁用代码分析?
- xml - SSIS 包未完全读取 XML
- knockout.js - 使用文本字符串而不是索引号与 Knockout.JS 进行拼接
- javascript - 为什么在 JavaScript 中的字符串之前有一个 + 会将其转换为整数?