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?

0

1 Answer 1

5

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>()
Sign up to request clarification or add additional context in comments.

7 Comments

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.
this doesn't seem to be OS specific. I had this issue in my windows for a while before i figured it out.
Btw, I figured out that on Windows, Visual Studio was using the launchSettings.json file, which already had "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } inside.
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
I even posted a question on this and put it on bounty stackoverflow.com/questions/48758249/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.