2

I have a function app, the app settings has several different PATs One of the function is an HTTP trigger, the request body will contain a specific name and that name should be used to retrieve the corresponding PAT from app settings

I have tried Environment.GetEnvironmentVariable, this doesnt seem to read from app settings at all

I have tried

var configuration = builder.GetContext().Configuration;
var pat = configuration[$"{name}_PAT"];

this was a struggle because the {name} is only available after the POST request comes in, and configuration read the app settings in startup.cs, which is before the function getting triggered

I also cant use configuration after the request had come in because of the {name} and public abstract void Configure(IFunctionsHostBuilder builder) from FunctionsStartup doesn't have any forms that would take a string that's needed for the {name}

5
  • configuration read the app settings in startup.cs, which is before the function getting triggered - maybe try converting a section from appsettings.json into a Dictionary? See stackoverflow.com/a/45673985/558486. Commented Jul 11, 2024 at 17:11
  • does this only work with appsettings.json? because the app settings I have all the PATs in is on portal. Creating a dictionary on the portal is the double underscore right? I assume it'd look like PAT__companyA, PAT__companyB, PAT__companyC. If i do a var pat = configuration["PAT"], would it retrieve all three of them? Commented Jul 11, 2024 at 17:18
  • If I remember correctly, yes you're correct. For example, if there is a PAT section like "PAT": { "Foo": "xxx", "Bar": "yyy" } then yes, you can access its values using environment variables PAT__Foo and PAT_Bar. Using this solution you should be able to create a dictionary for section PAT. Commented Jul 11, 2024 at 17:25
  • You can still declare that section in the app settings file, with empty or dummy values. Values defined in the portal will override them, if you don't make any mistake :-) Commented Jul 11, 2024 at 17:26
  • Another workaround: stackoverflow.com/a/41476230/558486 Commented Jul 11, 2024 at 17:43

1 Answer 1

0
  • I am using Environment.GetEnvironmentVariable to access {name}_PAT values from Function App's App settings.
  • Primarily, I added 2 variables as shown below in App settings.

enter image description here

  • Then I am using given code to fetch the value.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace _78736864
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            
            string requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            string name =  data?.name;

            string patKey = $"{name}_PAT";
            string pat = Environment.GetEnvironmentVariable(patKey);

            return new OkObjectResult(new { PAT = pat });
        }
    }
}
  • I am able to get the expected values upon function invocation.

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@nevermonday check the given solution. Hope this works for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.