

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定參數存放區
<a name="configure-store"></a>

參數存放區是 的功能 AWS Systems Manager。它為組態資料管理和秘密管理提供安全的階層式儲存。您可以將密碼、資料庫字串、Amazon Machine Image (AMI) ID，以及授權碼之類的資料存放為參數值。

## 搭配 .NET Framework 應用程式使用參數存放區的先決條件
<a name="configure-store-prereq"></a>
+ 作用中 AWS 帳戶
+ [Microsoft Visual Studio](https://visualstudio.microsoft.com/downloads/)，已安裝
+ AWS Command Line Interface (AWS CLI) 第 2 版，已安裝並設定為存取您的 AWS 帳戶 （請參閱[說明](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html))
+ AWS Toolkit for Visual Studio，已設定 （請參閱[說明](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/setup.html))
+ Systems Manager 參數，使用 [Secrets Manager 主控台](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-create-console.html)或 建立 [AWS CLI](https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-cli.html)

## 範例
<a name="configure-store-example"></a>

若要從 ASP.NET Core Web 應用程式或 API 中的參數存放區擷取值：

1. 將下列 NuGet 套件新增至 ASP.NET Core Web API。

   ```
   Amazon.Extensions.Configuration.SystemsManager
   ```

1. 在 `Program.cs` 檔案中，進行下列變更。
   + 新增`using`陳述式 (1)。

     ```
     using Amazon;
     using Amazon.Extensions.NETCore.Setup;
     ```
   + 新增 AWS Systems Manager 組態 (2)。

     ```
     builder.Configuration.AddSystemsManager("/dev/myapp", new AWSOptions
     {
         Region = RegionEndpoint.EUWest2
     });
     ```  
![\[用於存取參數存放區的 Program.cs 檔案變更\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/modernization-net-applications-security/images/ps-program-cs.png)
**注意**  
您應該動態呼叫 `/myapp/dev`和 `RegionEndPoint` 參數，或從環境變數 () 呼叫 和 參數`Region = RegionEndpoint.GetBySystemName("eu-west-2")`。請勿在生產環境中硬式編碼這些值。

1. 建立新的類別檔案並將其命名為 `ParameterOptions.cs`。開啟 檔案並新增下列程式碼。

   ```
   public class ParameterOptions
       {
           public const string ParameterName = "Tenant";
           public string key1 { get; set; } = string.Empty;
           public string key2 { get; set; } = string.Empty;
       }
   ```

1. 若要從參數存放區擷取值，請對控制器類別檔案進行下列變更 （例如，`ValuesController.cs`)。
   + 新增建構函數 (1)。

     ```
     private readonly IConfiguration _configuration;
     public ParametersController(IConfiguration configuration)
     {
         _configuration = configuration;
     }
     ```
   + 從參數存放區 (2) 擷取值。

     ```
     var parameterOptions = new ParameterOptions();
     _configuration.GetSection(ParameterOptions.ParameterName).Bind(parameterOptions);
     
     return new string[] {
         parameterOptions.key1,
         parameterOptions.key2
     };
     ```  
![\[從參數存放區擷取值的控制器類別檔案變更\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/modernization-net-applications-security/images/ps-controller-class.png)

## Resources
<a name="configure-store-resources"></a>
+ [AWS Secrets Manager 輪換 Lambda 函數 ](https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas)(GitHub 儲存庫）
+ [AWS Systems Manager 的 .NET 組態延伸，範例資料夾](https://github.com/aws/aws-dotnet-extensions-configuration/tree/master/samples/Samples) (GitHub 儲存庫）
+ [如何在 .NET 中使用 Secrets Manager 用戶端快取](https://aws.amazon.com/blogs/security/how-to-use-aws-secrets-manager-client-side-caching-in-dotnet/) (AWS 安全部落格）