Secret storage in .NET

Enable secret storage:
Type “dotnet user-secrets init” into Package Manager Console

<UserSecretsId> can be found in .csproj file.

Set a secret (Package Manager Console):
dotnet user-secrets set “ApiKey” “test123”

or to remove it:
dotnet user-secrets remove “ApiKey”

To check if exists:
dotnet user-secrets list
or
Manage secret through GUI -> right click on project -> Manage User Secrets -> it opens secrets.json

Location of this secrets.json file:
%APPDATA%\Microsoft\UserSecrets\<UserSecretsId>\secrets.json

secrets.json

Sample usage:
DI IConfiguration in constructor:
IConfiguration configuration

Field:
private IConfiguration _configuration;

Use it:
var test = _configuration[“ApiKey”];

Much more info:
https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-6.0&tabs=windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: