如何获得“管理用户机密"在 .NET Core 控制台应用程序中? [英] How to get "Manage User Secrets" in a .NET Core console-application?

查看:30
本文介绍了如何获得“管理用户机密"在 .NET Core 控制台应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个新的 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 :

https://medium.com/@granthair5/how-to-add-and-use-user-secrets-to-a-net-core-console-app-a0f169a8713f

tl;博士

第 1 步

右键项目并点击编辑 projectName.csproj

Right click project and hit edit projectName.csproj

第 2 步

在TargetFramework下的csproj中添加<UserSecretsId>Insert New Guid Here</UserSecretsId>

add <UserSecretsId>Insert New Guid Here</UserSecretsId> into csproj under TargetFramework

在csproj的Item Group中添加<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0"/>

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<YourClassName> 注入到你的类的构造函数中

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屋!

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