How do I retrieve secrets from AWS Secret Manager in .NET Core Console App?

Share

.NET Core/.NET console application contains a program.cs file, which has the Main method that runs when we execute the application. People normally use the console application executables for running smaller jobs or batch processing. In some scenarios, bigger applications might need configurations to be stored in a separate configuration file.

Click here to locate the sample application repository on GitHub.

When you create a console application using visual studio or using the command line, it doesn’t contain a configuration file by default. You need to create an appsettings.json file inside your project. 

This file contains your application configurations in JSON format as below,

{

  “Config”: {

    “ConnectionDetails”: {“Database”: “yourdatabase”,

                                          “Username”: “yourusername”,

                                          “Password”: “yourpassword” }}

}

Create Class structure to store these configurations in object form as below,

public class Config  { public ConnectionDetails connectionDetails { get; set; } }

public class ConnectionDetails {  public string Database { get; set; }

public string Username { get; set; }

public string Password { get; set; }}

Add below code block to consume appsettings.json into c# object,

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddJsonFile(“appsettings.json”, optional: false, reloadOnChange: true);

IConfigurationRoot configurationRoot = configurationBuilder.Build();

Config? config = configurationRoot.GetSection(“Config”).Get<Config>();

Create your AWS secrets in the below format and pass them to the console application as an environment variable,

{“Config:ConnectionDetails:Database”: “awsyourdatabase”,

  “Config:ConnectionDetails:Username”: “awsyourusername”}

Final code block to consume environment variable into c# object will become like below,

using Microsoft.Extensions.Configuration; using System.Text;

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddJsonFile(“appsettings.json”, optional: false, reloadOnChange: true);

//pass AWS secrets to executable as environment variable

string data = Environment.GetEnvironmentVariable(“Config”);

if (!string.IsNullOrEmpty(data) && data.Trim() != “”){

    MemoryStream newData = new(Encoding.Default.GetBytes(data));

    if (newData != null) configurationBuilder.AddJsonStream(newData);}

IConfigurationRoot configurationRoot = configurationBuilder.Build();

Config? config = configurationRoot.GetSection(“Config”).Get<Config>();

Console.WriteLine(“Database: ” + config.connectionDetails.Database);

Console.WriteLine(“Username: ” + config.connectionDetails.Username);

You might have observed that the format of the secret’s JSON stored in AWS will be the same as the user secrets JSON that you might have used for development purposes on your local machine. Learn more about the dotnet user-secrets here.


Share

Leave a Comment