SecretUserVars 1.1.0
dotnet add package SecretUserVars --version 1.1.0
NuGet\Install-Package SecretUserVars -Version 1.1.0
<PackageReference Include="SecretUserVars" Version="1.1.0" />
paket add SecretUserVars --version 1.1.0
#r "nuget: SecretUserVars, 1.1.0"
// Install SecretUserVars as a Cake Addin #addin nuget:?package=SecretUserVars&version=1.1.0 // Install SecretUserVars as a Cake Tool #tool nuget:?package=SecretUserVars&version=1.1.0
SecretUserVars
This is a very small library that provides a quick and simple way to add and maintain secrets that should be kept separate from a project. A good example of these secrets are API keys, usernames and passwords. The entire process is embedded into an application and runs only at the beginning. No third-party library is used. Only namespaces part of .NET 7 are used.
How to use the library
- Instantiate the
SecretsConfiguration
class with a dynamic file path (such as within local appdata) to work on platforms supported by .NET. - Add variables with the
AddVariable
method. Use the overwrite bool to force a read even if the variable already exists. - Configure the missing variables with the
ConfigureMissing
method. AnIVariableValueReader
must be provided for the source of variable values. A console implementation of the interface is provided for a quick start. - Save the secrets file so users don't have to input them again.
- Push the secrets into variables for use within the program.
Here is an example of reading existing variables from a file or, if they don't exist, reading them with the given IVariableValueReader
.
using SecretUserVars;
var secretsPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\test.json";
var secretsConfig = new SecretsConfiguration(secretsPath)
.AddVariable(key: "hello", overwriteExistingKey: false) // world
.AddVariable("leggo") // my eggo
.AddVariable("hugh") // mungus
.AddVariable("special number") // some number
.ConfigureMissing(new ConsoleVariableValueReader())
.SaveSecretsFile()
.PushVariableInto("hello", out var hello)
.PushVariableInto("leggo", out var leggo)
.PushVariableInto("hugh", out var hugh)
.PushVariableInto("special number", out var specialNumber, int.Parse);
Console.WriteLine(string.Join(Environment.NewLine, secretsConfig.EnvironmentVariables));
How it works
Internally, a dictionary backs all variables and is stored in JSON. Upon instantiation of the configuration class, the class will try to read existing variables from the given path. If the file does not exist, the dictionary will be empty. If the file is only partially filled with the required variables, the missing ones added will be configured.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.