问题描述
我正在尝试在.Net 4.5控制台应用程序上从Azure keyvault中获取机密信息,我遵循了This tuto,但标题中提到了错误请求错误。我不得不提到我的控制台应用程序是新创建的,并且正在使用属于另一个.Net核心Web api的clientId和ClientSecret,该api能够成功获取机密,但在我的控制台应用程序中并非如此。请在下面的代码中使用am,在client.GetSecretAsync(vaultAddress,SecretName)的指令处引发异常:
static void Main(string[] args)
{
Console.WriteLine($"Secret Value from Vault is: { GetVaultValue()}");
//DoVault();
Console.ReadLine();
}
static string GetVaultValue()
{
keyvaultClient client = new keyvaultClient(new keyvaultClient.AuthenticationCallback(GetToken));
var vaultAddress = "https://VaultName.vault.azure.net/";
var secretName = "storageName:NameAccount";
var secret = client.GetSecretAsync(vaultAddress,secretName).GetAwaiter().GetResult();
return secret.Value;
}
static async Task<string> GetToken(string authority,string resource,string scope)
{
var clientId = "This-IS-ID";
var clientSecret = "THIS-iS-my-Secret";
ClientCredential credential = new ClientCredential(clientId,clientSecret);
var context = new AuthenticationContext(authority,TokenCache.DefaultShared);
var result = await context.AcquiretokenAsync(resource,credential);
return result.Accesstoken;
}