

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

# 秘密管理
<a name="secrets"></a>

當您使用 .NET 開發應用程式時，您需要存放秘密，例如應用程式 ID、應用程式秘密和資料庫連線字串。這些秘密和連線字串存放在 `appsettings.json`或 中`web.config`。開發人員通常會將這些組態檔案新增至公有儲存庫，這會增加安全漏洞的風險。您可以使用 AWS Secrets Manager 和 參數存放區來降低此風險，這可協助您安全地存放和擷取應用程式的這些值。下列各節說明如何為 .NET Framework 應用程式設定 Secrets Manager 和 參數存放區。

**Topics**
+ [設定 AWS Secrets Manager](configure-asm.md)
+ [設定參數存放區](configure-store.md)

# 設定 AWS Secrets Manager
<a name="configure-asm"></a>

AWS Secrets Manager 可協助您保護存取應用程式、服務和 IT 資源所需的秘密。此服務可安全地存放、管理、加密和輪換資料庫憑證、API 金鑰和其他秘密，包括 OAuth 權杖，並提供與 Amazon Relational Database Service (Amazon RDS)、Amazon Redshift 和 Amazon DocumentDB 的原生整合。使用者和應用程式透過呼叫 Secrets Manager APIs 來擷取秘密，無需以純文字硬式編碼敏感資訊。Secrets Manager 包含精細存取控制許可，並提供集中位置來稽核秘密在 AWS 雲端內部部署和第三方環境中的輪換。

## 搭配 .NET Framework 應用程式使用 Secrets Manager 的先決條件
<a name="configure-asm-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))
+ 使用 Secrets Manager 主控台或 建立和擷取的秘密 AWS CLI （請參閱[說明](https://docs.aws.amazon.com/secretsmanager/latest/userguide/managing-secrets.html))

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

若要從 ASP.NET Core Web API (.NET 6) 中的 Secrets Manager 存取秘密：

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

   ```
   AWSSDK.SecretsManager.Caching
   ```

1. 在 `Program.cs` 檔案中，進行下列變更。
   + 新增`Amazon.SecretsManager`命名空間 (1)。

     ```
     using Amazon.SecretsManager;
     ```
   + 註冊服務 (2)。

     ```
     builder.Services.AddScoped<IAmazonSecretsManager>(x =>
           new AmazonSecretsManagerClient(RegionEndpoint.EUWest2)
        );
     ```  
![\[用於存取 Secrets Manager 的 Program.cs 檔案變更\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/modernization-net-applications-security/images/asm-program-cs.png)

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

     ```
     private readonly IAmazonSecretsManager _secretsManager;
     
     public SecretsController(IAmazonSecretsManager secretsManager)
     {
         _secretsManager = secretsManager;
     }
     ```
   + 實作 `GetSecret`方法 (2)。

     ```
     string secretName = "arn:aws:secretsmanager:eu-west-2:111122223333:secret:dev/myapp/tenant-gSj6qd";
     GetSecretValueRequest request = new GetSecretValueRequest();
     request.SecretId = secretName;
     request.VersionStage = "AWSCURRENT";
     Task<GetSecretValueResponse> response = _secretsManager.GetSecretValueAsync(request);
     return Ok(new { Secret = response.Result.SecretString });
     ```

     其中 *111122223333* 是指帳戶 ID。  
![\[從 Secrets Manager 擷取秘密的控制器類別檔案變更\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/modernization-net-applications-security/images/asm-controller-class.png)
**注意**  
`secretName` 是指秘密的名稱或 Amazon Resource Name (ARN)。建立秘密後，可以從 Secrets Manager 主控台擷取此值。您應該`secretName`動態呼叫 或從環境變數呼叫 。請勿在生產環境中硬式編碼此值。

# 設定參數存放區
<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 安全部落格）