/// <summary> /// <para> /// Adds the user secrets configuration source. This searches <paramref name="assembly"/> for an instance /// of <see cref="UserSecretsIdAttribute"/>, which specifies a user secrets ID. /// </para> /// <para> /// A user secrets ID is unique value used to store and identify a collection of secret configuration values. /// </para> /// </summary> /// <param name="configuration">The configuration builder.</param> /// <param name="assembly">The assembly with the <see cref="UserSecretsIdAttribute" />.</param> /// <param name="optional">Whether loading secrets is optional. When false, this method may throw.</param> /// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param> /// <exception cref="InvalidOperationException">Thrown when <paramref name="optional"/> is false and <paramref name="assembly"/> does not have a valid <see cref="UserSecretsIdAttribute"/>.</exception> /// <returns>The configuration builder.</returns> public static IConfigurationBuilder AddUserSecrets(this IConfigurationBuilder configuration, Assembly assembly, bool optional, bool reloadOnChange) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } UserSecretsIdAttribute attribute = assembly.GetCustomAttribute <UserSecretsIdAttribute>(); if (attribute != null) { return(AddUserSecrets(configuration, attribute.UserSecretsId, reloadOnChange)); } if (!optional) { throw new InvalidOperationException(SR.Format(SR.Error_Missing_UserSecretsIdAttribute, assembly.GetName().Name)); } return(configuration); }
/// <summary> /// Run the demo. /// </summary> public static void Run() { // run command helper action Action <string, string> runCommandAction = new Action <string, string>( (cmd, args) => { Console.WriteLine($"[Trace] running command: {cmd} {args}"); var startInfo = new ProcessStartInfo(cmd, args) { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true, }; Process process = new Process { StartInfo = startInfo }; process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); Console.WriteLine($"[Trace] {output}"); }); // set user secret helper action Action <string, string> setUserSecretAction = new Action <string, string>( (key, value) => runCommandAction("dotnet", $"user-secrets set {key} {value}") ); // set user secrets: // dotnet user-secrets set str_setting_1 str_value_1 setUserSecretAction("str_setting_1", "str_value_1"); // dotnet user-secrets set int_setting_1 1 setUserSecretAction("int_setting_1", "1"); // dotnet user-secrets set section1:nested_setting_1 nested_value_1 setUserSecretAction("section1:nested_setting_1", "nested_value_1"); Console.WriteLine(); IConfigurationBuilder configBuilder = new ConfigurationBuilder() .AddUserSecrets <UserSecretsConfigDemo>(); IConfiguration config = configBuilder.Build(); Action <string, Func <object> > getValueAction = (path, getValueFunc) => { object value = getValueFunc(); Console.WriteLine($"path: '{path}', value: '{value}'"); }; // get string value demo getValueAction( "str_setting_1", () => { string value = config["str_setting_1"]; return(value); }); // get nested string value demo, using ':' delimiter getValueAction( "section1:nested_setting_1", () => { string value = config["section1:nested_setting_1"]; return(value); }); // get int value demo, using GetValue<T> method getValueAction( "int_setting_1", () => { int value = config.GetValue <int>("int_setting_1", defaultValue: 0); return(value); }); // print user secrets file Console.WriteLine(); Assembly assembly = Assembly.GetExecutingAssembly(); UserSecretsIdAttribute attribute = assembly.GetCustomAttribute <UserSecretsIdAttribute>(); string userSecretsId = attribute.UserSecretsId; string userSecretsFilePath = PathHelper.GetSecretsPathFromSecretsId(userSecretsId); Console.WriteLine($"[Trace] User secrets file: {userSecretsFilePath}"); string userSecretsFileContent = File.ReadAllText(userSecretsFilePath, Encoding.UTF8); Console.WriteLine(userSecretsFileContent); }