如何获得“管理用户机密"在 .NET Core 控制台应用程序中?

编程入门 行业动态 更新时间:2024-10-25 00:28:16
本文介绍了如何获得“管理用户机密"在 .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 :

medium/@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 控制台应用程序中?

本文发布于:2023-11-14 09:10:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   机密   如何获得   应用程序   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!