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

查看:73
本文介绍了如何获取“管理用户秘密”;在.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-to-add-and-use-user-secrets-to-a-net-core-console-app-a0f169a8713f

tl; dr

步骤1

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

Right click project and hit edit projectName.csproj

第2步

添加< UserSecretsId>在此处插入新的向导到TargetFramework下的csproj

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

添加< DotNetCliToolReference Include = Microsoft.Extensions.SecretMana ger.Tools Version = 2.0.0 /> 在csproj的项目组中

add <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0"/> within Item Group in csproj

第3步

打开PowerShell(管理员)cd进入项目目录,然后

Open PowerShell (admin) cd into project directory and

输入 dotnet用户-秘密设置YourSecretName YourSecretContent

这将在以下位置创建secrets.json文件:

This will create a secrets.json file in:

%APPDATA%\microsoft\UserSecrets\<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< NameOfClass> ;(); 应该是 services.GetService< SecretConsumer>();

Thanks to Patric for pointing out that services.GetService<NameOfClass>(); should be services.GetService<SecretConsumer>();

这篇关于如何获取“管理用户秘密”;在.NET Core控制台应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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