User Secrets Not working in .Net Core 6.0 for Function Apps
Asked Answered
B

2

8

I have a time-triggered function app developed in .Net core 6.0, When I am using local.setting.Json to store configuration data. I am able to fetch the data using

System.Environment.GetEnvironmentVariable("variablename")

but when I shift this variable into the secrets.Json file (Secret Manager in .net Core) it returns me a null value.

Any help would be really appricated!

Bobettebobina answered 10/5, 2022 at 12:23 Comment(3)
Environment variables have nothing to do with secrets.json. That file is used by the Configuration middleware to store sensitive settings values during development so they aren't committed accidentally to a source repo. You aren't using the Configuration middleware at all. In a production environment secrets.json offers no security as the values aren't encrypted.Gawk
I think you mixed up the Azure Function's configuration with the ASP.NET Core application's configurationGawk
Thanks for the quick response, my code was working perfectly fine in .Net core 3.0. and able to fetch data from the Secret.Json file in the local development environment. this issue is only with 6.0.Bobettebobina
G
3

Here's how you do it.

Step 1 - Switch to IConfiguration to abstract the source(s) of your application settings. IConfiguration supports Environment Variables by default.

Step 2 - Manually add user secrets to IFunctionsConfigurationBuilder:

// Startup.cs
public class Startup : FunctionsStartup
{
    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        builder.ConfigurationBuilder.AddUserSecrets<Startup>();
    }
}

Step 3 - Use IConfiguration to fetch secrets:

this._configuration["Variable_Name"]

local.settings.json:

{
  "Values": {
    "Variable_Name": "[Secret]"
  }
}

secrets.json:

{
  "Variable_Name": "f0596f70d7af6f830f52b019cdb156e3"
}
Gastelum answered 9/1, 2023 at 20:25 Comment(2)
Tested on Azure Functions V4 with .NET 6.Gastelum
Don't update the Microsoft.Extensions.Configuration.UserSecrets to version 7... or the above code will throw exceptions.. You need to run in isolated mode to keep your functions up to date, but that's another crazy story ;)Abiding
G
0
  • We tried to reproduced the same scenario and we did it with secrets.json file with in azure function and now its is working as excepted.
  • You can use the following code to read secrets file value.

Example :- local.settings.json file

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"GetValue_from_local_settings": " local.settings.json file value"
  }
}

secrets.json file

{
"GetValue_from_Secrets": "Secrets.json file value"
}

Azure Function file

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;
using Microsoft.Extensions.Configuration;



namespace Secretsfrom_secretsJson
{
public class Function1
{
private readonly IConfiguration _configuration;



public Function1(IConfiguration dIConfiguration)
{
this._configuration = dIConfiguration;
}



[FunctionName("Function1")]
public string Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log )
{
string localsettingjson = System.Environment.GetEnvironmentVariable("GetValue_from_local_settings");
string Secrets_values = _configuration.GetSection("GetValue_from_Secrets").Value;
string result = "Local Setting value" + localsettingjson + " , Secrets value " + Secrets_values;
return result;
}
}
}



Find the values during application debugging .

enter image description here

output enter image description here

Gautier answered 2/6, 2022 at 13:29 Comment(1)
How is this working as expected? You are loading two separate values. Try loading a "connectionstring" which is defined in both local.settings and secrets. You will see that it won't display the value from the secrets.Reconstruct

© 2022 - 2024 — McMap. All rights reserved.