无法从 Azure Key Vault 中读取值

Can't read value from Azure Key Vault

我在我的 Azure Functions 应用程序中实施了 Azure Key Vault,遵循这篇文章: https://medium.com/statuscode/getting-key-vault-secrets-in-azure-functions-37620fd20a0b

如文章中所述,我正在使用托管服务标识 (MSI),但看起来我无法从 Key Vault 中读取值。以下是应该读取值的行。

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

这是我的条目在 Azure KeyVault 上的样子:

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

这是错误:

Exception while executing function: IngridNotificationsFunction Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: IngridNotificationsFunction ---> System.ArgumentNullException : Value cannot be null. Parameter name: secretIdentifier at async Microsoft.Azure.KeyVault.KeyVaultClientExtensions.GetSecretAsync(??)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at 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:\Users\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) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidTaskMethodInvoker.cs : 20 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker2.InvokeAsync[TReflected,TReturnValue](Object instance,Object[] arguments) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs : 63 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at 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 中读取值的原因可能是什么?

System.ArgumentNullException : Value cannot be null


根据异常说明,Environment.GetEnvironmentVariable("documentDbkey")为null。

What could be the reason why I'm unable to read values from my Azure KeyVault?

如果我们想使用 Environment.GetEnvironmentVariable("documentDbkey") 我们需要配置 azure 函数应用程序设置以添加值为 [= 的键 documentDbkey 12=] 在你的情况下。

更新:

您可以直接使用下面的代码获取秘钥

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

article 中还提到他使用应用程序设置来存储 key vault secret id。

You’ll notice I am using an environment variable (application setting) in this case for the key vault secret ID, but that itself is not a secret — just a location of where the secret is stored