首页 文章

无法从Azure Key Vault读取值

提问于
浏览
4

我在本文之后的Azure Functions应用程序中实现了Azure Key Vault:https://medium.com/statuscode/getting-key-vault-secrets-in-azure-functions-37620fd20a0b

如本文所述,我使用的是托管服务标识(MSI),但看起来我无法读取密钥保管库的值 . 以下是应该读取值的行 .

var myValue = (await kvClient.GetSecretAsync(Environment.GetEnvironmentVariable("documentDbkey"))).Value;

这是我的条目在Azure KeyVault上的样子:
enter image description here

我应该使用 key 作为我的条目,即 documentDb 或版本Id是以 bf2550f4e 开头的那个?

这是错误:

执行函数时出现异常:IngridNotificationsFunction Microsoft.Azure.WebJobs.Host.FunctionInvocationException:执行函数时出现异常:IngridNotificationsFunction ---> System.ArgumentNullException:Value不能为null . 参数名称:secretIdentifier at async Microsoft.Azure.KeyVault.KeyVaultClientExtensions.GetSecretAsync(??)at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()async Ingrid.Notifications.IngridNotifications.Initialize()at C:\ Users \ Sam \ Documents \ Visual Studio 2017 \ Projects \ Ingrid.Notifications \ Ingrid.Notifications \ IngridNotifications.cs:83 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at async Ingrid.Notifications.IngridNotifications.Run(String myQueueItem)at C:\用户\ Sam \ Documents \ Visual Studio 2017 \ Projects \ Ingrid.Notifications \ Ingrid.Notifications \ IngridNotifications.cs:38 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at async Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker2 .InvokeAsync [TReflected,TReturnType](TReflected instance,Object [] arguments)在C:\ projects \ azure-webjobs-sdk-rqm4t \ src \ Microsoft.Azure.WebJobs.Host \ Executors \ VoidTaskMethodInvoker.cs:20 at System . Runtime.ExceptionServices.ExceptionDi SpatchInfo.Throw()在异步Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker2.InvokeAsync [TReflected,TReturnValue](对象实例,对象[]参数)在C:\ projects \ azure-webjobs-sdk-rqm4t \ src \ Microsoft.Azure.WebJobs.Host \ Executors \ FunctionInvoker.cs:63 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource) timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance)at C:\ projects \ azure-webjobs-sdk-rqm4t \ src \ Microsoft.Azure.WebJobs.Hos ...

可能是我无法从Azure KeyVault读取值的原因是什么?

1 回答

  • 2

    System.ArgumentNullException:值不能为null


    根据例外,它表示 Environment.GetEnvironmentVariable("documentDbkey") 为空 .

    我可能无法从Azure KeyVault读取值的原因是什么?

    如果我们想使用 Environment.GetEnvironmentVariable("documentDbkey") ,我们需要配置azure功能应用程序设置,以便在您的情况下添加值为 https://{yourkeyvalue}.vault.azure.net/Secrets/{yourSecretName} 的密钥documentDbkey .

    enter image description here

    enter image description here

    Update:

    您可以直接使用以下代码来获取密码 .

    kvClient.GetSecretAsync("https://{yourkeyvalue}.vault.azure.net/Secrets/{yourSecretName}")​.Value
    

    enter image description here

    article中还提到他使用应用程序设置来存储密钥库的秘密ID .

    您会注意到我在这种情况下使用环境变量(应用程序设置)作为密钥保管库密钥ID,但这本身并不是秘密 - 只是存储密钥的位置

相关问题