Azure Keyvault 用于检索安全值

问题描述 投票:0回答:1
const string secretName = "MyConnectionString";
var kvUri = "https://{vaultname}.vault.azure.net/{}";

var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());

var secret = await client.GetSecretAsync(secretName);
Console.WriteLine($"Your secret is '{secret.Value.Value}'.");

我收到错误:

http 404 - 未找到文件或目录。
您正在查找的资源可能已被删除、更名或暂时不可用。

azure azure-keyvault
1个回答
0
投票

http 404 - 未找到文件或 > 目录。 您正在查找的资源可能已被删除、更名或暂时不可用。

上述错误404 - 未找到文件或目录表示您尝试访问的资源可能已被删除、更名或暂时不可用。请确保您为变量

secretName
kvUri
提供了正确的值。

在我的环境中,我传递了具有正确的保管库名称和秘密名称的参数来检索秘密值。

代码:

using Azure.Identity;
using Azure.Security.KeyVault.Secrets;

class Program
{
    static void Main(string[] args)

    {
        string vaultName = "<Your-keyvault-name>";
        string secretName = "MyConnectionString";
        var kvUri = $"https://{vaultName}.vault.azure.net/";
        var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
        var secret = client.GetSecret(secretName);
        Console.WriteLine($"Your secret is '{secret.Value.Value}'.");
    }
}

上述代码用于从 Azure Key Vault 检索机密。它创建一个

SecretClient
对象,并使用
DefaultAzureCredential
和 Azure Key Vault 的 URI 进行身份验证。然后使用
GetSecret
方法从 Azure Key Vault 检索具有指定名称的机密。

输出:

Your secret is 'Hellomyworld'.

enter image description here

参考: 快速入门 - 适用于 .NET 的 Azure Key Vault 机密客户端库 |微软学习

© www.soinside.com 2019 - 2024. All rights reserved.