Azure KeyVault在运行getSecret()运行时给出了null引用异常

发布于 2025-02-02 11:57:05 字数 667 浏览 4 评论 0原文

当我尝试从Azure KeyVault获取API Secret时,我会收到一个无参考错误。我在密钥维护中设置了密钥,但是秘密以null返回。

    public static string GetKeyInformation(API_KEY)
    {
        if (string.IsNullOrEmpty(API_KEY))
        {
            var keyVaultUrl = "https://socialflutter.vault.azure.net/";

            var credential = new DefaultAzureCredential();

            var client = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential);

            KeyVaultSecret secret = client.GetSecret();

            Console.WriteLine($"{secret.Name}: {secret.Value}");

            API_KEY = secret.Value;
        }

        return API_KEY;
    }

任何帮助将不胜感激。

When I try to get the API secret from Azure KeyVault, I am receiving a null reference error. I have the Key set up in the KeyVault, but secret is coming back as null.

    public static string GetKeyInformation(API_KEY)
    {
        if (string.IsNullOrEmpty(API_KEY))
        {
            var keyVaultUrl = "https://socialflutter.vault.azure.net/";

            var credential = new DefaultAzureCredential();

            var client = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential);

            KeyVaultSecret secret = client.GetSecret();

            Console.WriteLine(
quot;{secret.Name}: {secret.Value}");

            API_KEY = secret.Value;
        }

        return API_KEY;
    }

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2025-02-09 11:57:05

由于以下行,正在发生空引用错误:

KeyVaultSecret secret = client.GetSecret();

即使您只有一个秘密定义,Azure KeyVault也不知道如何读取键。

您将需要以下语法:

KeyVaultSecret secret = client.GetSecret(<KEY_NAME>);

要查找&lt; key_name&gt;,请参阅下面的屏幕截图:

  1. 转到键库
  2. 单击秘密,
  3. 复制秘密的名称

“

The null reference error is happening because of the following line:

KeyVaultSecret secret = client.GetSecret();

Even if you just have a single secret defined, Azure KeyVault will not know how to read the Key.

You will want the following syntax:

KeyVaultSecret secret = client.GetSecret(<KEY_NAME>);

To find the <KEY_NAME>, refer to the screenshot below:

  1. Go to Key Vault
  2. Click on Secrets
  3. Copy the Name of the Secret

Azure KeyVault

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文