我有以下代码用于从 Azure 密钥保管库获取机密:
public static async Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
ClientCredential clientCred = new ClientCredential(...); //app id, app secret
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token");
return result.AccessToken;
}
public static string GetSecret(string secretName)
{
KeyVaultClient keyVaultClient = new KeyVaultClient(GetToken);
try
{
return keyVaultClient.GetSecretAsync("my-key-vault-url", secretName).Result.Value;
}
catch(Exception ex)
{
return "Error";
}
}
我得到的错误是“访问被拒绝”,这(我认为)意味着 id、secret 和保管库的 url 都很好。但是,我不知道我可以采取哪些不同的方法来修复此错误,Azure 门户中是否存在阻止我阅读机密的设置?