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

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

僅使用 .NET 應用程式的 SSO 教學課程

本教學課程說明如何為基本應用程式和測試 SSO 使用者啟用 SSO。它會將應用程式設定為以程式設計方式產生暫存 SSO 憑證,而不是使用. AWS CLI

本教學課程會顯示中的一小部分 SSO 功能AWS SDK for .NET。如需搭配使用 IAM 身分中心的完整詳細資訊AWS SDK for .NET,請參閱包含背景資訊的主題。在該主題中,請特別參閱名為的子節中此案例的高階描述僅. NET 應用程式

注意

本教學課程中的幾個步驟可協助您設定AWS Organizations和 IAM 身分中心等服務。如果您已經執行了該配置,或者您只對代碼感興趣,則可以跳到包含示例代碼的部分。

先決條件

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

  • 識別或建立至少AWS 帳戶一個可用來測試 SSO。對於本教程的目的,這被稱為測試AWS 帳戶或只是測試帳戶

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

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

設定 AWS

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

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

Amazon S3

轉到 Amazon S3 控制台並添加一些無害的存儲桶。在本教學課程稍後,SSO 使用者將擷取這些值區的清單。

AWSIAM

前往 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 身分中心主控台並啟用 SSO。必要時執行電子郵件驗證。如需詳細資訊,請參閱 IAM 身分中心使用者指南中的啟用 IAM 身分中心

然後,執行以下配置。

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

  2. 在的大標題中AWS Management Console,尋找啟用 SSO 時設定的。AWS 區域這是 AWS 帳戶 ID 左側的下拉菜單。記錄地區碼,以便稍後在sso_region設定中使用。此代碼將類似於us-east-1.

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

    1. 前往「使用者」頁面。

    2. 選擇 [新增使用者],然後輸入使用者的使用者稱、電子郵件地址字和姓氏。然後選擇 Next (下一步)。

    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 使用者的電腦上執行。

包括 NuGet 套件AWSSDK.SSO,以AWSSDK.S3及除了和AWSSDK.SecurityTokenAWSSDK.SSOOIDC

using System; using System.Threading.Tasks; using System.Diagnostics; // 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.Programmatic_login { class Program { // Requirements: // - An SSO profile in the SSO user's shared config file. // 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"); var ssoCredentials = credentials as SSOAWSCredentials; ssoCredentials.Options.ClientName = "Example-SSO-App"; ssoCredentials.Options.SsoVerificationCallback = args => { // Launch a browser window that prompts the SSO user to complete an SSO login. // This method is only invoked if the session doesn't already have a valid SSO token. // NOTE: Process.Start might not support launching a browser on macOS or Linux. If not, // use an appropriate mechanism on those systems instead. Process.Start(new ProcessStartInfo { FileName = args.VerificationUriComplete, UseShellExecute = true }); }; return ssoCredentials; } } // 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; } } }

包括 NuGet 套件AWSSDK.SSO,以AWSSDK.IdentityManagement及除了和AWSSDK.SecurityTokenAWSSDK.SSOOIDC

using System; using System.Threading.Tasks; using System.Diagnostics; // 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.Programmatic_login { class Program { // Requirements: // - An SSO profile in the SSO user's shared config file. // 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"); var ssoCredentials = credentials as SSOAWSCredentials; ssoCredentials.Options.ClientName = "Example-SSO-App"; ssoCredentials.Options.SsoVerificationCallback = args => { // Launch a browser window that prompts the SSO user to complete an SSO login. // This method is only invoked if the session doesn't already have a valid SSO token. // NOTE: Process.Start might not support launching a browser on macOS or Linux. If not, // use an appropriate mechanism on those systems instead. Process.Start(new ProcessStartInfo { FileName = args.VerificationUriComplete, UseShellExecute = true }); }; return ssoCredentials; } } // 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 使用者清單之外,這些應用程式還會顯示啟用 SSO 設定檔的使用者身分 ARN,這些設定檔位於本教學my-sso-profile中。

這些應用程式會在 SSO AWSCredentials 物件的 [選項] 屬性中提供回呼方法,以執行 SSO 登入工作。

指示 SSO 使用者

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

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

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

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

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

  2. 如有必要,請在該資料夾中建立共享AWSconfig檔案,然後依照下列步驟在其中新增設定檔:

    [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. 在產生的網頁登入頁面中,登入。使用邀請郵件中的使用者名稱,以及為了回應訊息而建立的密碼。

  5. 登入完成後,應用程式會顯示 S3 儲存貯體的清單。

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

清除

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