Can't access User Secrets in ASP.net core 3.0
Asked Answered
B

5

6

My question is simple. I have an ASP.net core 3.0 app, I added secrets using visualstudio and pasted my secrets into the secret file normally. Then inside my Program.cs, I added a call to addusersecrets as follows:

...
...
.AddUserSecrets<Startup>()

But while calling my secrets like Configuration["Authentication:Secret"] as I used to do when it was in appsettings.json, I get a null value in return.

I went through stackoverflow and tried solutions like changing my addsecrets as follows:

.AddUserSecrets("fds...-...-...askd")

//OR

.AddUserSecrets(typeof(Startup).Assembly, true, reloadOnChange: true)

BUt none of then works.

I wonder if this secret stuff even works on asp.net core, because I don't see any reason my code doesn't work. please if someone gets it, can you tell me a solution ? Thanks.

Benenson answered 15/11, 2019 at 10:18 Comment(0)
A
4

Did your code look like this ?

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", 
                     optional: false, 
                     reloadOnChange: true)
        .AddEnvironmentVariables();

    if (env.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }

    Configuration = builder.Build();
}
About answered 15/11, 2019 at 10:39 Comment(4)
yeah it did. it is similar to that.Benenson
can you show me how you read your config file in your c# codeAbout
Hi, I do the build in my Program.cs instead it looks like this: var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) .AddEnvironmentVariables() .AddUserSecrets<Startup>(true, reloadOnChange: true) .Build(); Benenson
Thanks, you made me realise I was running my configs in a different way, adding my configs in the startup.cs and calling the user secrets there helped me.Benenson
C
5

Be sure to check that you've set the "ASPNETCORE_ENVIRONMENT" variable to "Development", else your app wont add your user secrets.

  • in powershell:

    $Env:ASPNETCORE_ENVIRONMENT = "Development"

  • cmd:

    setx ASPNETCORE_ENVIRONMENT "Development"

Compensate answered 8/2, 2020 at 13:12 Comment(2)
What will be the approach say for e.g a app in the production environment?Gamosepalous
@David -- I know your question is old, but you do not use "user secrets"/secrets.json in a production environment. It is strictly for local development. You'd want to use Azure Key Vault or Azure App Configuration for storing production secrets, keys, connection strings, etc.Morena
A
4

Did your code look like this ?

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", 
                     optional: false, 
                     reloadOnChange: true)
        .AddEnvironmentVariables();

    if (env.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }

    Configuration = builder.Build();
}
About answered 15/11, 2019 at 10:39 Comment(4)
yeah it did. it is similar to that.Benenson
can you show me how you read your config file in your c# codeAbout
Hi, I do the build in my Program.cs instead it looks like this: var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) .AddEnvironmentVariables() .AddUserSecrets<Startup>(true, reloadOnChange: true) .Build(); Benenson
Thanks, you made me realise I was running my configs in a different way, adding my configs in the startup.cs and calling the user secrets there helped me.Benenson
P
2

For .Net 6 that doesn't use Startup one loads the assembly such as

build.AddUserSecrets(Assembly.GetExecutingAssembly())
Padauk answered 20/4, 2022 at 13:41 Comment(0)
F
0

In Asp.Net MVC 5, add this to Program.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, configuration) =>
                {
                    configuration.Sources.Clear();
                    configuration.AddEnvironmentVariables();
                    configuration.AddUserSecrets(Assembly.GetExecutingAssembly(), true, true);
                    
                })
                .UseSystemd()
                .UseSerilog((context, service, configuration) => {
                    configuration
                    .MinimumLevel.Warning()
                    .Enrich.FromLogContext()
                    .WriteTo.Console();

                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
 
                    webBuilder.UseStartup<Startup>();
                });
                
    }
Fatherly answered 28/3, 2023 at 11:8 Comment(0)
B
-2

User secrets are stored now in csproj project file. This is how you can read it(see image). I am using asp.net core 5 instructions

Blindstory answered 14/12, 2020 at 14:59 Comment(1)
I need to access the user-secrets for a project that does not contain a Startup.cs.Furriery

© 2022 - 2024 — McMap. All rights reserved.