Skip to content

Instantly share code, notes, and snippets.

@ukcoderj
Created May 5, 2020 13:09
Show Gist options
  • Save ukcoderj/935e7d9bde1eb71ae1adfeb0eadb9db6 to your computer and use it in GitHub Desktop.
Save ukcoderj/935e7d9bde1eb71ae1adfeb0eadb9db6 to your computer and use it in GitHub Desktop.
.NET Core Console Accessing User Secrets
using Microsoft.Extensions.Configuration;
using System;
namespace TestSecrets
{
// NuGet
//
// Microsoft.Extensions.Configuration
// Microsoft.Extensions.Configuration.UserSecrets
// Add key/values to usersecrets.json
class Program
{
private static IConfigurationRoot Configuration { get; set; }
static void Main(string[] args)
{
BootstrapConfiguration();
Console.WriteLine("Hello World!");
var test = Configuration["TestSecret"];
Console.WriteLine($"secret is {test}");
}
/// <summary>
/// This is a helper to set up access to the UserSecrets file
/// </summary>
private static void BootstrapConfiguration()
{
string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.IsNullOrWhiteSpace(env))
{
env = "Development";
}
var builder = new ConfigurationBuilder();
if (env == "Development")
{
builder.AddUserSecrets<Program>();
}
Configuration = builder.Build();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment