Microsoft.Azure.KeyVault.Models.KeyVaultErrorException:'操作返回了无效的状态码'BadRequest'

时间:2020-08-20 15:55:41

标签: c# .net azure console-application azure-keyvault

我正在尝试在.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;
    }

1 个答案:

答案 0 :(得分:1)

<UI open={open}> {/*children here*/} </UI> 包含冒号secretName,导致此错误。

根据我的测试,不允许在密钥库名称中包含冒号:

enter image description here

请检查并更正。