ASP 核心无法在 VS 2017 中设置用户机密

ASP Core Cannot Set User Secrets in VS 2017

对于 visual studio 2017,当我尝试设置用户密码时出现以下错误:

> dotnet user-secrets set Authentication:Google:ClientId mysecretclientid
> Could not find the global property 'UserSecretsId' in MSBuild project 'c:\test\myproj.csproj'. Ensure this property is set in the project or use the '--id' command line option.

我的 Startup.cs 里有这个:

[assembly: UserSecretsId("project-8084c8e7-0000-0000-b266-b33f42dd88c0")]

...

builder.AddUserSecrets<Startup>();

如果我将其添加到我的 csproj 中:

  <PropertyGroup>
    <UserSecretsId>project-8084c8e7-0000-0000-b266-b33f42dd88c0</UserSecretsId>
  </PropertyGroup>

我收到一条错误消息

Duplicate 'Microsoft.Extensions.Configuration.UserSecrets.UserSecretsIdAttribute'

我做错了什么?

好的,所以我让它工作了,看起来在 VS 2017 中你应该删除“[assembly: UserSecretsId("project-8084c8e7-0000-0000-b266-b33f42dd88c0")]”属性并在 csproj 文件中包含以下内容:

<PropertyGroup>
    <UserSecretsId>project-8084c8e7-0000-0000-b266-b33f42dd88c0</UserSecretsId>
</PropertyGroup>

如果您使用外部缓存提供程序(例如 redis),请将其放入您的 .csproj 文件中 <PropertyGroup><UserSecretsId>Redistest</UserSecretsId></PropertyGroup>
Redistest 是秘密名称。可以是任何东西。

我遇到了同样的错误,我通过生成新的 UserSecretsId 修复了它。

如果您在解决方案资源管理器中右键单击项目并按 Manage User Secrets,将自动生成 UserSecretsId。

在 VS Code 2019 中,您可以使用以下命令在您的 csproj 文件中生成 UserSecret 部分。 运行 命令时,请确保您位于包含所需 csproj 文件的目录中。

dotnet user-secrets init