

第 4 版 (V4) 適用於 .NET 的 AWS SDK 已發行！

如需有關中斷變更和遷移應用程式的資訊，請參閱[遷移主題](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)。

 [https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)

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

# 使用舊版憑證
<a name="net-dg-legacy-creds"></a>

本節中的主題提供有關在不使用 AWS IAM Identity Center的情況下使用長期或短期憑證資訊。

**警告**  
為避免安全風險，在開發專用軟體或使用真實資料時，請勿使用 IAM 使用者進行身分驗證。相反地，搭配使用聯合功能和身分提供者，例如 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html)。

**注意**  
本主題中的資訊適用於您需要手動取得及管理短期或長期憑證的情況。有關短期和長期憑證的其他資訊，請參閱 *AWS SDK 和工具參考指南*中的[其他驗證方法](https://docs.aws.amazon.com/sdkref/latest/guide/access-users.html)。  
如需最佳實務，請使用 AWS IAM Identity Center，如 中所述[使用 驗證 AWS](creds-idc.md)。

## 憑證的重要警告和指引
<a name="net-dg-config-creds-warnings-and-guidelines"></a>

**憑證警告**
+ ***請勿使用***您帳戶的根登入資料來存取 AWS 資源。這些登入資料可讓未管制的帳戶存取和很難撤銷這些帳戶。
+ ***請勿***在應用程式檔案中放置常值存取金鑰或憑證資訊。如果您不小心這麼做了，則會有暴露您登入資料的風險，例如，當您上傳專案到公有儲存庫時。
+ ***請勿在***您的專案區域中放入包含憑證的檔案。
+ 請注意，存放在共用 AWS `credentials`檔案中的任何登入資料都會以純文字儲存。

**安全管理憑證的其他指引**

如需如何安全管理 AWS 登入資料的一般討論，請參閱《》中的[AWS 安全登入](https://docs.aws.amazon.com/general/latest/gr/Welcome.html#aws-security-credentials)資料[AWS 一般參考](https://docs.aws.amazon.com/general/latest/gr/)和《[IAM 使用者指南](https://docs.aws.amazon.com/IAM/latest/UserGuide/)》中的[安全最佳實務和使用案例](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAMBestPracticesAndUseCases.html)。除了這些討論之外，請考慮下列事項：
+ 建立其他使用者 (例如 IAM Identity Center 中的使用者)，並使用其憑證，而不是使用您的 AWS 根使用者憑證。如有必要，其他使用者的憑證可以被撤銷，或本質上是臨時的。此外，您可以將政策套用至每個使用者，以便僅存取特定資源和動作，從而採取最低權限許可的立場。
+ 使用適用於 Amazon Elastic Container Service (Amazon ECS) 任務的[任務 IAM 角色](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html)。
+ 使用在 Amazon EC2 執行個體上執行的應用程式的 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)。
+ 為組織外部使用者可用的應用程式使用[臨時登入](creds-assign.md#net-dg-config-creds-assign-role)資料或環境變數。

**Topics**
+ [憑證的重要警告和指引](#net-dg-config-creds-warnings-and-guidelines)
+ [使用共用 AWS 登入資料檔案](creds-file.md)
+ [使用 SDK 存放區 （僅限 Windows)](sdk-store.md)

# 使用共用 AWS 登入資料檔案
<a name="creds-file"></a>

（請務必檢閱[登入資料的重要警告和指引](net-dg-legacy-creds.md#net-dg-config-creds-warnings-and-guidelines)。)

為您的應用程式提供登入資料的一種方法是在*共用 AWS 登入資料檔案中*建立設定檔，然後將登入資料存放在這些設定檔中。此檔案可供其他 AWS SDKs使用。它也可以由 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/)、 和適用於 [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/)[AWS Tools for Windows PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/)、[JetBrains](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/) 和 [VS 程式碼](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/) AWS 的工具組使用。

**警告**  
為避免安全風險，在開發專用軟體或使用真實資料時，請勿使用 IAM 使用者進行身分驗證。相反地，搭配使用聯合功能和身分提供者，例如 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html)。

**注意**  
本主題中的資訊適用於您需要手動取得及管理短期或長期憑證的情況。有關短期和長期憑證的其他資訊，請參閱 *AWS SDK 和工具參考指南*中的[其他驗證方法](https://docs.aws.amazon.com/sdkref/latest/guide/access-users.html)。  
如需最佳實務，請使用 AWS IAM Identity Center，如 中所述[使用 驗證 AWS](creds-idc.md)。

## 一般資訊
<a name="creds-file-general-info"></a>

根據預設，共用的 AWS 登入資料檔案位於主`.aws`目錄中的 目錄中，並命名為 `credentials`；也就是 `~/.aws/credentials`(Linux 或 macOS) 或 `%USERPROFILE%\.aws\credentials`(Windows)。如需有關替代位置的資訊，請參閱 *[AWS SDKs和工具參考指南](https://docs.aws.amazon.com/sdkref/latest/guide/overview.html)*中的[共用檔案的位置](https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html)。另請參閱[在應用程式中存取登入資料和設定檔](creds-locate.md)。

共用的 AWS 登入資料檔案是純文字檔案，並遵循特定格式。如需 AWS 登入資料檔案格式的資訊，請參閱 *AWS SDKs和工具參考指南*中的[登入資料檔案格式](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html#file-format-creds)。

您可以透過多種方式管理共用 AWS 登入資料檔案中的設定檔。
+ 使用任何文字編輯器來建立和更新共用的 AWS 登入資料檔案。
+ 使用 適用於 .NET 的 AWS SDK API 的 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間，如本主題稍後所示。
+ 使用適用於 [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/credentials.html)、[JetBrains](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/setup-credentials.html) 和 VS Code 的 [AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html)和 AWS 工具組命令和程序。 [https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-credentials.html](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-credentials.html)
+ 使用 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html) 命令，例如 `aws configure set aws_access_key_id`和 `aws configure set aws_secret_access_key`。

## 設定檔管理的範例
<a name="creds-file-examples"></a>

下列各節顯示共用 AWS 登入資料檔案中的設定檔範例。部分範例顯示結果，可透過上述任何登入資料管理方法取得。其他範例示範如何使用特定方法。

### 預設設定檔
<a name="creds-file-default"></a>

共用的 AWS 登入資料檔案幾乎一律會有名為*預設值*的設定檔。如果未定義其他設定檔，則 會在這裡 適用於 .NET 的 AWS SDK 尋找登入資料。

`[default]` 設定檔通常如下所示。

```
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
```

### 以程式設計方式建立設定檔
<a name="creds-file-create-programmatically"></a>

此範例說明如何建立設定檔，並以程式設計方式將其儲存至共用的 AWS 登入資料檔案。它使用下列類別的 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間： [CredentialProfileOptions](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TCredentialProfileOptions.html)、 [CredentialProfile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TCredentialProfile.html) 和 [SharedCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TSharedCredentialsFile.html)。

```
using Amazon.Runtime.CredentialManagement;
...

// Do not include credentials in your code.
WriteProfile("my_new_profile", SecurelyStoredKeyID, SecurelyStoredSecretAccessKey);
...

void WriteProfile(string profileName, string keyId, string secret)
{
    Console.WriteLine($"Create the [{profileName}] profile...");
    var options = new CredentialProfileOptions
    {
        AccessKey = keyId,
        SecretKey = secret
    };
    var profile = new CredentialProfile(profileName, options);
    var sharedFile = new SharedCredentialsFile();
    sharedFile.RegisterProfile(profile);
}
```

**警告**  
這類程式碼通常不應在您的應用程式中。如果您將其包含在應用程式中，請採取適當的預防措施，以確保無法在程式碼中、透過網路或甚至電腦記憶體中看到純文字金鑰。

以下是此範例建立的設定檔。

```
[my_new_profile]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
```

### 以程式設計方式更新現有的設定檔
<a name="creds-file-update-programmatically"></a>

此範例說明如何以程式設計方式更新先前建立的設定檔。它使用下列類別的 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間： [CredentialProfile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TCredentialProfile.html) 和 [SharedCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TSharedCredentialsFile.html)。它也會使用 [Amazon](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Amazon/N.html) 命名空間的 [RegionEndpoint](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Amazon/TRegionEndpoint.html) 類別。

```
using Amazon.Runtime.CredentialManagement;
...

AddRegion("my_new_profile", RegionEndpoint.USWest2);
...

void AddRegion(string profileName, RegionEndpoint region)
{
    var sharedFile = new SharedCredentialsFile();
    CredentialProfile profile;
    if (sharedFile.TryGetProfile(profileName, out profile))
    {
        profile.Region = region;
        sharedFile.RegisterProfile(profile);
    }
}
```

以下是更新的設定檔。

```
[my_new_profile]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
region=us-west-2
```

**注意**  
您也可以使用其他方法在其他位置設定 AWS 區域。如需詳細資訊，請參閱[設定 AWS 的區域 適用於 .NET 的 AWS SDKAWS 區域](net-dg-region-selection.md)。

# 使用 SDK 存放區 （僅限 Windows)
<a name="sdk-store"></a>

（請務必檢閱[重要的警告和指導方針](net-dg-legacy-creds.md#net-dg-config-creds-warnings-and-guidelines)。)

在 Windows 上，*SDK Store* 是另一個為您的 適用於 .NET 的 AWS SDK 應用程式建立設定檔和存放加密登入資料的地方。它位於 中`%USERPROFILE%\AppData\Local\AWSToolkit\RegisteredAccounts.json`。您可以在開發期間使用 SDK Store 做為[共用 AWS 登入資料檔案](creds-file.md)的替代方案。

**警告**  
為避免安全風險，在開發專用軟體或使用真實資料時，請勿使用 IAM 使用者進行身分驗證。相反地，搭配使用聯合功能和身分提供者，例如 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html)。

**注意**  
本主題中的資訊適用於您需要手動取得及管理短期或長期憑證的情況。有關短期和長期憑證的其他資訊，請參閱 *AWS SDK 和工具參考指南*中的[其他驗證方法](https://docs.aws.amazon.com/sdkref/latest/guide/access-users.html)。  
如需最佳實務，請使用 AWS IAM Identity Center，如 中所述[使用 驗證 AWS](creds-idc.md)。

## 一般資訊
<a name="sdk-store-general-info"></a>

開發套件存放區提供下列優點：
+ 軟體開發套件存放區中的登入資料會加密，而軟體開發套件存放區位於使用者的主目錄中。此可限制意外暴露您的登入資料的風險。
+ 軟體開發套件存放區也提供登入資料給 [AWS Tools for Windows PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/)和 [AWS Toolkit for Visual Studio](https://docs.aws.amazon.com/AWSToolkitVS/latest/UserGuide/)。

軟體開發套件存放區設定檔是特定主機上特定使用者特有的。您無法複製它們給其他的主機或其他的使用者。這表示您無法為其他主機或開發人員機器重複使用開發機器上的 SDK Store 設定檔。這也表示您無法在生產應用程式中使用 SDK Store 設定檔。

您可以透過下列方式管理 SDK Store 中的設定檔：
+ 使用 中的圖形使用者介面 (GUI)[AWS Toolkit for Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/credentials.html)。
+ 使用 適用於 .NET 的 AWS SDK API 的 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間，如本主題稍後所示。
+ 使用來自 的命令[AWS Tools for Windows PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html)，例如 `Set-AWSCredential`和 `Remove-AWSCredentialProfile`。

## 設定檔管理的範例
<a name="sdk-store-examples"></a>

下列範例示範如何在 SDK Store 中以程式設計方式建立和更新設定檔。

### 以程式設計方式建立設定檔
<a name="sdk-store-create-programmatically"></a>

此範例說明如何建立設定檔，並以程式設計方式將其儲存至 SDK 存放區。它使用下列類別的 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間： [CredentialProfileOptions](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TCredentialProfileOptions.html)、[CredentialProfile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TCredentialProfile.html) 和 [NetSDKCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TNetSDKCredentialsFile.html)。

```
using Amazon.Runtime.CredentialManagement;
...

// Do not include credentials in your code.
WriteProfile("my_new_profile", SecurelyStoredKeyID, SecurelyStoredSecretAccessKey);
...

void WriteProfile(string profileName, string keyId, string secret)
{
    Console.WriteLine($"Create the [{profileName}] profile...");
    var options = new CredentialProfileOptions
    {
        AccessKey = keyId,
        SecretKey = secret
    };
    var profile = new CredentialProfile(profileName, options);
    var netSdkStore = new NetSDKCredentialsFile();
    netSdkStore.RegisterProfile(profile);
}
```

**警告**  
這類程式碼通常不應在您的應用程式中。如果您的應用程式包含 ，請採取適當的預防措施，以確保無法在程式碼中、透過網路或甚至電腦記憶體中看到純文字金鑰。

以下是此範例建立的設定檔。

```
"[generated GUID]" : {
    "AWSAccessKey" : "01000000D08...[etc., encrypted access key ID]",
    "AWSSecretKey" : "01000000D08...[etc., encrypted secret access key]",
    "ProfileType"  : "AWS",
    "DisplayName"  : "my_new_profile",
}
```

### 以程式設計方式更新現有的設定檔
<a name="sdk-store-update-programmatically"></a>

此範例說明如何以程式設計方式更新先前建立的設定檔。它使用下列類別的 [Amazon.Runtime.CredentialManagement](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/NRuntimeCredentialManagement.html) 命名空間： [CredentialProfile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TCredentialProfile.html) 和 [NetSDKCredentialsFile](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Runtime/TNetSDKCredentialsFile.html)。它也會使用 [Amazon](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Amazon/N.html) 命名空間的 [RegionEndpoint](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/Amazon/TRegionEndpoint.html) 類別。

```
using Amazon.Runtime.CredentialManagement;
...

AddRegion("my_new_profile", RegionEndpoint.USWest2);
...

void AddRegion(string profileName, RegionEndpoint region)
{
    var netSdkStore = new NetSDKCredentialsFile();
    CredentialProfile profile;
    if (netSdkStore.TryGetProfile(profileName, out profile))
    {
        profile.Region = region;
        netSdkStore.RegisterProfile(profile);
    }
}
```

以下是更新的設定檔。

```
"[generated GUID]" : {
    "AWSAccessKey" : "01000000D08...[etc., encrypted access key ID]",
    "AWSSecretKey" : "01000000D08...[etc., encrypted secret access key]",
    "ProfileType"  : "AWS",
    "DisplayName"  : "my_new_profile",
    "Region"       : "us-west-2"
}
```

**注意**  
您也可以使用其他方法在其他位置設定 AWS 區域。如需詳細資訊，請參閱[設定 AWS 的區域 適用於 .NET 的 AWS SDKAWS 區域](net-dg-region-selection.md)。