您的位置:首页 > 编程语言 > ASP

[译]ASP.NET Core 2.0 机密配置项

2017-10-26 09:40 597 查看

问题

如何在ASP.NET Core 2.0中保存机密配置项(不用将其暴露给源代码管理器)?

答案

创建一个ASP.NET Core 2.0空项目,在项目节点上点击右键,并点击菜单项 - 管理用户机密:



这将会打开secrets.json文件,添加配置键值对:

{
"UserPassword": "Password1"
}


为此配置项添加POCO类:

public class SecretSettings
{
public string UserPassword { get; set; }
} 


使用之前创建的HelloWorldMiddleware中间件,将IOptions<T>作为中间件的构造函数参数注入,其中T就是我们刚刚定义的POCO类:

public class HelloWorldMiddleware
{
private readonly RequestDelegate _next;
private readonly SecretSettings _settings;

public HelloWorldMiddleware(RequestDelegate next, IOptions<SecretSettings> options)
{
_next = next;
_settings = options.Value;
}

public async Task Invoke(HttpContext context)
{
var jsonSettings = JsonConvert.SerializeObject(_settings, Formatting.Indented);
await context.Response.WriteAsync(jsonSettings);
}
}

public static class UseHelloWorldInClassExtensions
{
public static IApplicationBuilder UseHelloWorld(this IApplicationBuilder app)
{
return app.UseMiddleware<HelloWorldMiddleware>();
}
}  


在Startup.cs中,我们需要做如下几件事:

1. 通过构造函数参数注入IConfiguration

2. 在ConfigureServices()中添加Options服务,并添加机密配置的依赖项

3. 在Configure()方法中使用中间件

public class Startup
{
public static IConfiguration Configuration { get; private set; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();

services.Configure<SecretSettings>(Configuration);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHelloWorld();
}
}  


运行,此时页面显示:



讨论

之前我们讨论过如何在配置文件中存储全局配置项。然而,这些配置文件会被签入源代码管理器,因此不适合用于保存机密配置。在生产环境中,这些配置可以保存到环境变量或者Azure云的密钥存储库中 。对于开发环境,ASP.NET Core 2.0提供了可选的解决方案:用户机密管理器。

用户机密管理器允许开发人员将机密信息保存到secrets.json文件中,而不会签入到源代码管理器。secrets.json文本被保存到系统的AppData目录中,在VS2017中你可以将鼠标移动到相应的选项卡上查看文件路径。需要注意的一点:机密信息是被保存在普通文本文件中的。这些文件是在创建WebHost时由运行时读取并加载的。

====start by sanshi=========================



你可能也注意到了用户机密文件路径中的那个类似GUID的字符串了,它是由VS 2017自动创建的,并存在工程文件中(SecretConfiguration.csproj):

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<UserSecretsId>2d08d295-6b15-46b3-a5a3-ad0b1992f492</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>

</Project>


====end by sanshi=========================

CLI

我们也可以使用命令行界面(CLI)的指令 dotnet user-secrets 来管理机密信息。为此,我们需要首先想工程文件中添加如下配置:

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
</ItemGroup>  


接下来,我们就可以使用如下命令来管理用户机密信息:

1. list:列出所有的机密信息,例如:dotnet user-secrets list

2. set:添加或更新某个机密项,例如:dotnet user-secrets set SecretSetting “SecretValue”

3. remove:删除某个机密项,例如:dotnet user-secrets remove SecretSetting

4. clear:清空所有机密项,例如:dotnet user-secrets clear

====start by sanshi=========================

下面,我们会简单演示这一过程,首先打开命令行窗体(Windows+X),并定位到项目所在目录:

cd C:\Users\sanshi\Desktop\ASP.NET_Core_20_Articles\SecretConfiguration\SecretConfiguration


然后键入如下命令:

dotnet user-secrets list  


运行结果:



下面来修改这个机密项:

dotnet user-secrets set UserPassword "My New Password"




在VS中打开机密文件,发现已经修改成功:

{
"UserPassword": "My New Password"
}


====end by sanshi=========================

源代码下载

原文:https://tahirnaushad.com/2017/08/31/asp-net-core-2-0-secret-manager/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: