1

I'm using dotnet-user-secrets with an ASP.NET Core 2.1.4 app.

To be clear, this question is about invoking dotnet run from the command line in macOS; this issue does not exist when invoking dotnet run for the same application in Windows. Additionally, running the app from within Visual Studio works as expected on both macOS and Windows.

Typing dotnet user-secrets list at the command line returns the expected output:

TokenProviderOptions:SecretKey = …
TokenProviderOptions:Issuer = …
TokenProviderOptions:Audience = …

I can also access the secrets.json file in the ~/.microsoft/usersecrets/<userSecretsId> folder as expected:

{
  "TokenProviderOptions": {
    "SecretKey": "…",
    "Issuer": "…",
    "Audience": "…"
  }
}

Here is how I am accessing the secrets inside Startup.cs:

// Configure tokens
var tokenOptions = Configuration
    .GetSection("TokenProviderOptions")
    .GetChildren()
    .ToList();
string
    secretKey = tokenOptions.Single(x => x.Key.Equals("SecretKey")).Value,
    issuer = tokenOptions.Single(x => x.Key.Equals("Issuer")).Value,
    audience = tokenOptions.Single(x => x.Key.Equals("Audience")).Value;

As I mentioned before, this runs with no problems from Command Prompt and Visual Studio in Windows. And, from Visual Studio in macOS, I can debug and see the tokenOptions list with the three items. However, when I type dotnet run in Terminal, I get this error at the first .Single():

System.InvalidOperationException: Sequence contains no matching element

Is there an extra step I'm missing to run this successfully from the command line in macOS?

crgolden
  • 4,332
  • 1
  • 22
  • 40

1 Answers1

4

The secret manager is designed to only work on development. So, it only works as far as your ASPNETCORE_ENVIRONMENT is set to Development. This can be done in your environment variables.

If you do not wish to modify your environment variables, you can use the following in your program.cs file:

public static IWebHost BuildWebHost(string[] args)
{

    var config = new ConfigurationBuilder().AddUserSecrets("myid").Build();

    return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
            .UseStartup<Startup>()
Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79
  • 1
    Thank you for the quick response! I added `ASPNETCORE_ENVIRONMENT=Development` to my `.bash_profile` and now it works perfectly. Perhaps this environment variable is added to Windows automatically during installation... not sure - just glad to have it working on my macOS now as well. – crgolden May 01 '18 at 14:36
  • this doesn't seem to be OS specific. I had this issue in my windows for a while before i figured it out. – Neville Nazerane May 01 '18 at 16:19
  • 1
    Btw, I figured out that on Windows, Visual Studio was using the `launchSettings.json` file, which already had `"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }` inside. – crgolden May 14 '18 at 02:22
  • yep, I have noticed that. This is what caused a lot of confusion in the past. While this file lets the website work fine, this file is not used for other commands like ef migrations. That was the main reason why I had to set that env variable – Neville Nazerane May 14 '18 at 02:33
  • I even posted a question on this and put it on bounty https://stackoverflow.com/questions/48758249/ef-core-migration-cant-use-secret-manager – Neville Nazerane May 14 '18 at 02:36
  • Just a quick remark, when updating the .bash_profile restart your terminal otherwise will not work :) – Fernando Moreira Jul 18 '18 at 15:18
  • Same goes for windows as well. You will have to restart cmd and PowerShell if you update the variable via CLI – Neville Nazerane Jul 18 '18 at 18:19