2

我使用函数运行时版本 2.0 编写的 azure 函数并尝试从 azure key vault 中读取机密。

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 包

当我使用 VS 本地 CLI 运行该函数并且该函数未运行并出现错误时。 命令行错误

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

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

4

1 回答 1

3

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

它已在最新的函数运行时2.0.11857(又名 cli 2.0.1-beta.29)中修复。请参阅此 github 问题拉取请求

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

此版本尚未提供给 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)

在此处输入图像描述

于 2018-06-15T11:42:35.933 回答