0

I have a json file creds.json as

:

I am using IConfiguration to read creds.json using the following:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, configuration) =>
            { 
                configuration.AddJsonFile("creds.json", optional: true, reloadOnChange: true);                      
            }                   
                

My Doubt is: Once I have injected the creds.json file in my project, How do I access it and read the username and password values???

Thanks.

abhishek
  • 29
  • 4
  • You can read options as usually. Does this answer your question? [How to read AppSettings values from a .json file in ASP.NET Core](https://stackoverflow.com/questions/31453495/how-to-read-appsettings-values-from-a-json-file-in-asp-net-core) – Lukasz Szczygielek Nov 12 '21 at 14:03

1 Answers1

0
public class VaultController : ControllerBase
{
    public IConfiguration Configuration { get; }
    public VaultController(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    [HttpGet("/api/readVault")]
    public string GetVaultDetails()
    {
        var Username = Configuration.GetValue<string>("username");
        var Password = Configuration.GetValue<string>("password");

        str = "name is:" + Username + " password is:" + Password;
        return str;                    
    }
}
abhishek
  • 29
  • 4