[譯]ASP.NET Core 2.0 機密配置項

三生石上(FineUI控制元件)發表於2017-10-26

問題

如何在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/

相關文章