azure function v 2.0和key vault

时间:2018-06-14 11:28:58

标签: azure azure-functions azure-functions-runtime

我的azure函数使用Function runtime Version 2.0编写并尝试从azure密钥库中读取秘密。

public class KeyVaultAccess
{

    private const string VaulturL = "...";
    private const string ClientId = "...";
    private const string thumbprinT = "...";

    public static string GetSecretIdentifier(string secretName)
    {
        string nextLink = VaulturL + "secrets" + "/" + secretName;
        var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken));
        var getsecrets = Task.Run(async () => await keyVaultClient.GetSecretsAsync(VaulturL));
        Task.WaitAll(getsecrets);

        if (getsecrets.Result == null)
            throw new Exception("Error retrieving secret name from key vault");
        var secret = keyVaultClient.GetSecretAsync(nextLink).GetAwaiter().GetResult();
        var GetSecretvalue = secret.Value;
        return GetSecretvalue;
    }

    private static async Task<string> GetAccessToken(string authority, string resource, string scope)
    {
        var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
        var clientcred = new ClientCredential(ClientId, thumbprinT);
        var result = await context.AcquireTokenAsync(resource, clientcred);
        return result.AccessToken;
    }
}

我在函数中引用了下面的nuget包。 Nuget Packages

当我使用VS本地CLI运行该功能并且该功能未运行并收到错误时。 CLI Error

仅供参考,相同的代码在Azure Function V1.0中运行良好。

任何线索我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

错误是由Microsoft.IdentityModel.Clients.ActiveDirectory的引用引起的。

它已在最新的函数运行时2.0.11857(又名cli 2.0.1-beta.29)中修复。请参阅this github issuepull request

在门户网站上发布已完成,但正如release note所说

  

此版本尚未提供给Visual Studio用户。

因此,您可以手动下载cli beta.29(win-x64或x86,基于您的平台)并配置以下调试设置。

Launch: Executable
Executable: C:\Program Files\dotnet\dotnet.exe (set your dotnet path)
Application Arguments: [yourclifolderpath]\Azure.Functions.Cli.win-x64\func.dll start
Working Directory: $(TargetDir)

enter image description here