azureazure-functionsazure-keyvault

How escape an = when getting a value from Azure Key Vault for Azure Functions


I have an Azure Function with an environment variable set to obtain the value from a key vault:

@Microsoft.KeyVault(VaultName=<vaultName>;SecretName=<secretName>)

We are getting issues when the value in the key vault has an "=" in the secret, e.g.

thisisaverygoodsecret=

If we store the secret against the environment variable directly, the application works as expected. I assume we need to escape the = in some way, but I can't see anything in the Microsoft docs.

We have several other Function variables obtained in the same way, so there is no problem with the link to the Key Vault.


Solution

  • I have added @Microsoft.KeyVault(VaultName=<vaultName>;SecretName=<secretName>) in App Settings which has = in its value.

    I have used Environment.GetEnvironmentVariable("secretValue"); to fetch the secret value from key vault and got the expected response.

    #r "Newtonsoft.Json"
    
    using System.Net;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;
    
    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
    
        string secretValue = Environment.GetEnvironmentVariable("secretValue");
    
        string name = req.Query["name"];
    
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;
    
        string responseMessage = string.IsNullOrEmpty(name)
            ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                    : $"Hello, {name}. The secret value is: {secretValue}";
    
                return new OkObjectResult(responseMessage);
    }
    

    enter image description here