具有“用户秘密"的ASP.NET Core应用;仅在macOS命令行上失败 [英] ASP.NET Core app with "user-secrets" fails on macOS command line only

查看:34
本文介绍了具有“用户秘密"的ASP.NET Core应用;仅在macOS命令行上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dotnet-user-secrets 带有ASP.NET Core 2.1.4应用.

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

要清楚,这个问题是关于在macOS中从命令行调用dotnet run的;在Windows中为同一应用程序调用dotnet run时,不存在此问题.此外,在macOS和Windows上都可以按预期在Visual Studio中运行该应用程序.

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.

在命令行中键入dotnet user-secrets list会返回预期的输出:

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

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

我也可以按预期访问~/.microsoft/usersecrets/<userSecretsId>文件夹中的secrets.json文件:

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

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

这是我访问Startup.cs中的秘密的方式:

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;

正如我之前提到的,在Windows中,命令提示符和Visual Studio都不会出现问题.而且,从macOS的Visual Studio中,我可以调试并查看包含三个项目的tokenOptions列表.但是,当我在终端中键入dotnet run时,在第一个.Single()处会出现此错误:

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

要从macOS的命令行中成功运行此步骤,我还缺少额外的步骤吗?

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

推荐答案

秘密管理器仅用于开发.因此,仅将ASPNETCORE_ENVIRONMENT设置为Development才有效.这可以在您的环境变量中完成.

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.

如果您不想修改环境变量,则可以在program.cs文件中使用以下内容:

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>()

这篇关于具有“用户秘密"的ASP.NET Core应用;仅在macOS命令行上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆