如何获取“管理用户机密"在 .NET Core 控制台应用程序中? [英] How to get "Manage User Secrets" in a .NET Core console-application?
问题描述
当我创建一个新的 ASP .NET Core Web-应用程序时,我可以在 Visual Studio 中右键单击该项目,然后我会看到一个名为管理用户机密"的上下文菜单条目.
When I create a new ASP .NET Core Web-Application, I can right-click the project in Visual Studio, and I see a context-menu entry called "Manage User Secrets".
当我创建一个新的 .NET Core 控制台-应用程序时,我没有看到这个上下文菜单条目.
When I create a new .NET Core Console-Application, I don't see this context-menu entry.
但是,Web"-应用程序在项目设置中显示为控制台"应用程序.有什么办法可以在控制台应用程序中获得这个上下文菜单条目吗?
However, a "Web"-Application shows as "console" application in the project settings. Is there any way I can get this context-menu entry in a console-application ?
推荐答案
通过右键单击管理用户机密"仅在 Web 项目中可用.
"Manage user secrets" from a right click is only available in web projects.
控制台应用程序的流程略有不同
There is a slightly different process for console applications
它需要手动将所需的元素输入到您的 csproj 文件中,然后通过 PMC 添加机密
It requires manually typing the required elements into your csproj file then adding secrets through the PMC
我在这篇博文中逐步概述了在我当前项目中对我有用的过程:
I have outlined the process that worked for me in my current project step by step in this blog post :
tl;dr
第一步
右键单击项目并点击编辑 projectName.csproj
Right click project and hit edit projectName.csproj
第 2 步
将
添加到 TargetFramework 下的 csproj
add <UserSecretsId>Insert New Guid Here</UserSecretsId>
into csproj under TargetFramework
在 csproj 的项目组中添加
add <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0"/>
within Item Group in csproj
步骤 3
打开 PowerShell (admin) cd 进入项目目录和
Open PowerShell (admin) cd into project directory and
输入 dotnet user-secrets set YourSecretName "YourSecretContent"
这将在以下位置创建一个 secrets.json 文件:
This will create a secrets.json file in:
%APPDATA%microsoftUserSecrets<userSecretsId>secrets.json
其中 userSecretsId = 您为 csproj 创建的新 Guid
Where userSecretsId = the new Guid you created for your csproj
步骤 4
打开secrets.json并编辑成类似这样
Open secrets.json and edit to look similar to this
{
"YourClassName":{
"Secret1":"Secret1 Content",
"Secret2":"Secret2 Content"
}
}
通过添加您的类的名称,您可以将您的秘密绑定到要使用的对象.
By adding the name of your class you can then bind your secrets to an object to be used.
创建一个与您刚刚在 JSON 中使用的名称相同的基本 POCO.
Create a basic POCO with the same name that you just used in your JSON.
namespace YourNamespace
{
public class YourClassName
{
public string Secret1 { get; set; }
public string Secret2 { get; set; }
}
}
步骤 5
将 Microsoft.Extensions.Configuration.UserSecrets
Nuget 包添加到项目
Add Microsoft.Extensions.Configuration.UserSecrets
Nuget package to project
添加
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddUserSecrets<YourClassName>()
.AddEnvironmentVariables();
&
var services = new ServiceCollection()
.Configure<YourClassName>(Configuration.GetSection(nameof(YourClassName)))
.AddOptions()
.BuildServiceProvider();
services.GetService<SecretConsumer>();
到您的 Program.cs 文件.
To your Program.cs file.
然后将 IOptions
注入到你的类的构造函数中
Then inject IOptions<YourClassName>
into the constructor of your class
private readonly YourClassName _secrets;
public SecretConsumer(IOptions<YourClassName> secrets)
{
_secrets = secrets.Value;
}
然后使用 _secrets.Secret1;
感谢 Patric 指出 services.GetService
应该是 services.GetService
Thanks to Patric for pointing out that services.GetService<NameOfClass>();
should be services.GetService<SecretConsumer>();
这篇关于如何获取“管理用户机密"在 .NET Core 控制台应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!