Prakash Srinivasan
Prakash Srinivasan

Reputation: 25

azure function v 2.0 and key vault

My azure function written using Function runtime Version 2.0 and trying to read secrets from 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;
    }
}

I have referenced below nuget packages in the function. Nuget Packages

when i run the function using VS local CLI and the function not running and getting the error. CLI Error

FYI, the same code is working fine in Azure Function V1.0.

Any clue what i am doing wrong here?

Upvotes: 2

Views: 506

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

The error is caused by the reference of Microsoft.IdentityModel.Clients.ActiveDirectory.

It has been fixed in latest function runtime 2.0.11857(aka cli 2.0.1-beta.29). See this github issue and pull request.

Release is complete on portal, but as the release note says

This release has not yet been made available to Visual Studio users.

So you can download cli beta.29 manually (win-x64 or x86, based on your platform) and configure following debug settings.

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

Upvotes: 3

Related Questions