使用 AWS CLI 和 .SSO 應用程式的 NET 教學課程 - AWS SDK for .NET

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

使用 AWS CLI 和 .SSO 應用程式的 NET 教學課程

本教學課程示範如何為基本 SSONET 應用程式和測試 Word 使用者啟用 SSO。它使用 AWS CLI 產生臨時 SSO 字符,而不是以程式設計方式產生它

本教學課程為您展示 中一小部分 SSO 功能 AWS SDK for .NET。如需搭配使用 IAM Identity Center 與 的完整詳細資訊 AWS SDK for .NET,請參閱具有背景資訊的主題。在該主題中,請參閱名為 的子節中此案例的高階描述AWS CLI和. NET 應用程式

注意

本教學課程中的幾個步驟可協助您設定 AWS Organizations 和 IAM Identity Center 等服務。如果您已執行這些組態,或者只對程式碼感興趣,則可以使用範例程式碼跳至 區段。

必要條件

  • 如果您尚未設定開發環境,請進行設定。這在 安裝和設定您的工具鏈和 等章節中描述開始使用

  • 識別或建立至少一個 AWS 帳戶 可用於測試 SSO 的 。在本教學課程中,這稱為 測試 AWS 帳戶或簡單測試帳戶

  • 識別可以為您測試 SSO 的 Word 使用者。 SSO這是將使用 SSO 和您建立的基本應用程式的人員。在本教學課程中,該人員可能是您 (開發人員) 或其他人。我們也建議 SSO 使用者在不在開發環境中的電腦上工作的設定。不過,這並非絕對必要。

  • SSO 使用者的電腦必須安裝與您用來設定開發環境的架構相容的 .NET 架構。

  • 請確定 AWS CLI 版本 2 已安裝在 SSO 使用者的電腦上。您可以透過在命令提示字元或終端機aws --version中執行 來檢查此問題。

設定 AWS

本節說明如何設定本教學課程的各種 AWS 服務。

若要執行此設定,請先以管理員 AWS 帳戶 身分登入測試。然後,執行下列動作:

Amazon S3

前往 Amazon S3 主控台並新增一些無害的儲存貯體。在本教學課程稍後,SSO 使用者將擷取這些儲存貯體的清單。

AWS IAM

前往 IAM 主控台並新增幾個 IAM 使用者。如果您授予 IAM 使用者許可,請將許可限制為幾個無害的唯讀許可。在本教學課程稍後,SSO 使用者將擷取這些 IAM 使用者的清單。

AWS Organizations

前往AWS Organizations 主控台並啟用 Organizations。如需詳細資訊,請參閱《AWS Organizations 使用者指南》https://docs.aws.amazon.com/organizations/latest/userguide/中的建立組織

此動作會將測試新增至 AWS 帳戶 組織作為管理帳戶。如果您有其他測試帳戶,您可以邀請他們加入組織,但此教學課程不需要這樣做。

IAM Identity Center

前往 IAM Identity Center 主控台並啟用 SSO。視需要執行電子郵件驗證。如需詳細資訊,請參閱 IAM Identity Center 使用者指南中的啟用 IAM Identity Center

然後,執行下列組態。

  1. 前往設定頁面。尋找「存取入口網站URL」,並記錄 值以供稍後在 sso_start_url設定中使用。

  2. 在 橫幅中 AWS Management Console,尋找在您啟用 AWS 區域 SSO 時設定的 。這是 AWS 帳戶 ID 左側的下拉式選單。記錄區域碼,以供稍後在 sso_region 設定中使用。此程式碼將類似於 us-east-1

  3. 建立 SSO 使用者,如下所示:

    1. 前往使用者頁面。

    2. 選擇新增使用者,然後輸入使用者的使用者名稱電子郵件地址名字姓氏。然後選擇下一步

    3. 在 頁面上選擇群組的下一步,然後檢閱資訊,然後選擇新增使用者

  4. 建立群組,如下所示:

    1. 前往群組頁面。

    2. 選擇建立群組,然後輸入群組的群組名稱描述

    3. 將使用者新增至群組區段中,選取您先前建立的測試 SSO 使用者。然後,選取建立群組。

  5. 建立許可集,如下所示:

    1. 前往許可集頁面,然後選擇建立許可集

    2. 許可集類型下,選取自訂許可集,然後選擇下一步

    3. 開啟內嵌政策並輸入下列政策:

      { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "iam:ListUsers" ], "Resource": "*" } ] }
    4. 在此教學課程中,輸入 SSOReadOnlyRole作為許可集名稱。如果您想要,請新增描述,然後選擇下一步

    5. 檢閱資訊,然後選擇建立

    6. 記錄許可集的名稱,以供稍後在 sso_role_name設定中使用。

  6. 前往AWS 帳戶頁面,然後選擇您先前新增至組織的 AWS 帳戶。

  7. 在該頁面的概觀區段中,尋找帳戶 ID,並記錄在sso_account_id設定中以供稍後使用。

  8. 選擇使用者和群組索引標籤,然後選擇指派使用者或群組

  9. 指派使用者和群組頁面上,選擇群組索引標籤,選擇您先前建立的群組,然後選擇下一步

  10. 選取您先前建立的許可集,然後選擇下一步,然後選擇提交。組態需要一些時間。

建立範例應用程式

建立下列應用程式。它們將在 SSO 使用者的電腦上執行。

除了 AWSSDK.SSOAWSSDK.SSOOIDC之外,還包含 NuGet 套件 AWSSDK.S3AWSSDK.SecurityToken

using System; using System.Threading.Tasks; // NuGet packages: AWSSDK.S3, AWSSDK.SecurityToken, AWSSDK.SSO, AWSSDK.SSOOIDC using Amazon.Runtime; using Amazon.Runtime.CredentialManagement; using Amazon.S3; using Amazon.S3.Model; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; namespace SSOExample.S3.CLI_login { class Program { // Requirements: // - An SSO profile in the SSO user's shared config file. // - An active SSO Token. // If an active SSO token isn't available, the SSO user should do the following: // In a terminal, the SSO user must call "aws sso login --profile my-sso-profile". // Class members. private static string profile = "my-sso-profile"; static async Task Main(string[] args) { // Get SSO credentials from the information in the shared config file. var ssoCreds = LoadSsoCredentials(profile); // Display the caller's identity. var ssoProfileClient = new AmazonSecurityTokenServiceClient(ssoCreds); Console.WriteLine($"\nSSO Profile:\n {await ssoProfileClient.GetCallerIdentityArn()}"); // Display a list of the account's S3 buckets. // The S3 client is created using the SSO credentials obtained earlier. var s3Client = new AmazonS3Client(ssoCreds); Console.WriteLine("\nGetting a list of your buckets..."); var listResponse = await s3Client.ListBucketsAsync(); Console.WriteLine($"Number of buckets: {listResponse.Buckets.Count}"); foreach (S3Bucket b in listResponse.Buckets) { Console.WriteLine(b.BucketName); } Console.WriteLine(); } // Method to get SSO credentials from the information in the shared config file. static AWSCredentials LoadSsoCredentials(string profile) { var chain = new CredentialProfileStoreChain(); if (!chain.TryGetAWSCredentials(profile, out var credentials)) throw new Exception($"Failed to find the {profile} profile"); return credentials; } } // Class to read the caller's identity. public static class Extensions { public static async Task<string> GetCallerIdentityArn(this IAmazonSecurityTokenService stsClient) { var response = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest()); return response.Arn; } } }

除了 AWSSDK.SSOAWSSDK.SSOOIDC之外,還包含 NuGet 套件 AWSSDK.IdentityManagementAWSSDK.SecurityToken

using System; using System.Threading.Tasks; // NuGet packages: AWSSDK.IdentityManagement, AWSSDK.SecurityToken, AWSSDK.SSO, AWSSDK.SSOOIDC using Amazon.Runtime; using Amazon.Runtime.CredentialManagement; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; using Amazon.SecurityToken; using Amazon.SecurityToken.Model; namespace SSOExample.IAM.CLI_login { class Program { // Requirements: // - An SSO profile in the SSO user's shared config file. // - An active SSO Token. // If an active SSO token isn't available, the SSO user should do the following: // In a terminal, the SSO user must call "aws sso login --profile my-sso-profile". // Class members. private static string profile = "my-sso-profile"; static async Task Main(string[] args) { // Get SSO credentials from the information in the shared config file. var ssoCreds = LoadSsoCredentials(profile); // Display the caller's identity. var ssoProfileClient = new AmazonSecurityTokenServiceClient(ssoCreds); Console.WriteLine($"\nSSO Profile:\n {await ssoProfileClient.GetCallerIdentityArn()}"); // Display a list of the account's IAM users. // The IAM client is created using the SSO credentials obtained earlier. var iamClient = new AmazonIdentityManagementServiceClient(ssoCreds); Console.WriteLine("\nGetting a list of IAM users..."); var listResponse = await iamClient.ListUsersAsync(); Console.WriteLine($"Number of IAM users: {listResponse.Users.Count}"); foreach (User u in listResponse.Users) { Console.WriteLine(u.UserName); } Console.WriteLine(); } // Method to get SSO credentials from the information in the shared config file. static AWSCredentials LoadSsoCredentials(string profile) { var chain = new CredentialProfileStoreChain(); if (!chain.TryGetAWSCredentials(profile, out var credentials)) throw new Exception($"Failed to find the {profile} profile"); return credentials; } } // Class to read the caller's identity. public static class Extensions { public static async Task<string> GetCallerIdentityArn(this IAmazonSecurityTokenService stsClient) { var response = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest()); return response.Arn; } } }

除了顯示 Amazon S3 儲存貯體和 IAM 使用者的清單之外,這些應用程式還會顯示啟用 ARN 的設定檔的使用者身分 SSO,這在本教學my-sso-profile課程中。

指示 SSO 使用者

請 SSO 使用者檢查其電子郵件並接受 SSO 邀請。系統會提示他們設定密碼。訊息可能需要幾分鐘的時間才能到達 SSO 使用者的收件匣。

為 SSO 使用者提供您先前建立的應用程式。

然後,請 SSO 使用者執行下列操作:

  1. 如果包含共用 AWS config檔案的資料夾不存在,請建立它。如果資料夾確實存在且具有名為 的子資料夾.sso,請刪除該子資料夾。

    此資料夾的位置通常%USERPROFILE%\.aws位於 Windows 和 Linux 和 macOS ~/.aws中。

  2. 視需要在該資料夾中建立共用 AWS config檔案,並將設定檔新增至該資料夾,如下所示:

    [default] region = <default Region> [profile my-sso-profile] sso_start_url = <user portal URL recorded earlier> sso_region = <Region code recorded earlier> sso_account_id = <account ID recorded earlier> sso_role_name = SSOReadOnlyRole
  3. 執行 Amazon S3 應用程式。出現執行期例外狀況。

  4. 執行下列 AWS CLI 命令:

    aws sso login --profile my-sso-profile
  5. 在產生的 Web 登入頁面中,登入。使用邀請訊息中的使用者名稱,以及為回應訊息而建立的密碼。

  6. 再次執行 Amazon S3 應用程式。應用程式現在會顯示 S3 儲存貯體的清單。

  7. 執行 IAM 應用程式。應用程式會顯示 IAM 使用者的清單。即使未執行第二次登入,也是如此。IAM 應用程式會使用先前建立的臨時權杖。

清除

如果您不想保留在本教學課程中建立的資源,請清除它們。這些可能是開發環境中 AWS 的資源或資源,例如檔案和資料夾。