0

I am retrieving secret information from KeyVault in the ConfigureServices method of my Startup class. How to transfer the data from Startup & access it in the service class?

This is what I am doing & this doesn't seem to work

services.AddSingleton<ClassName>(config) 

I am trying to access it in service class constructor by defining a parameter ClassName config.

Here config is an object of type ClassName.

3
  • 1
    Can you add how your constructor looks? What error or exception are you getting? Also how are you preparing the config object? Commented Apr 8 at 20:50
  • What is the version of your .NET Core? Commented Apr 9 at 7:34
  • Retrieve the secrets i nProgram.cs / startup.cs file, register the service using AddSingleton. Later you can call the service class some where in the controller to dispaly the secrets. Commented Apr 9 at 9:13

1 Answer 1

0

I am using .NET Core 8, there is no Startup file in my Solution. All the configuration and startup code are written directly in the Program.cs file itself.

  • Install Azure.Identity and Azure.Security.KeyVault.Secrets latest NuGet packages.

  • In appsettings.json, set the Key Vault URI.

  "KVUri": "https://KVName.vault.azure.net/"
  • Create a new class file serviceTest.cs.
 public class serviceTest
{
    public class Secrets
    {
        public string Sec1 { get; set; }
        public string Sec2 { get; set; }
    }
    private readonly Secrets _config;

    public serviceTest(Secrets config)
    {
        _config = config;
    }

    public void GetSecrets()
    {
        Console.WriteLine("Secret1: " + _config.Sec1);
        Console.WriteLine("Secret2: " + _config.Sec2);
    }  
}
  • Retrieve the secrets in Program.cs file and store them in the custom configuration Object Secrets.

My Program.cs file:

builder.Services.AddControllersWithViews();
var KVURI = builder.Configuration["KVUri"]; 
var client = new SecretClient(new Uri(KVURI), new DefaultAzureCredential());

var val1 = client.GetSecret("SampleKey").Value.Value;
var val2 = client.GetSecret("TestKey").Value.Value;
var secretConfig = new serviceTest.Secrets
{
    Sec1 = val1,
    Sec2 = val2
};
  • Register the serviceTest class using AddSingleton.
builder.Services.AddSingleton(secretConfig).AddSingleton<serviceTest>();
  • Now you can call the serviceTest class to retrieve the secrets somewhere in the code (Controller in my case)
 private readonly serviceTest _st;
 public HomeController(ILogger<HomeController> logger, serviceTest myService)
 {
     _logger = logger;
     _st = myService;
 }
  public IActionResult Index()
  {
      _st.GetSecrets();
      return View();
  }

Output:

enter image description here

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

Comments

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.