使用 SSO 的教程Amazon CLI和.NET 应用程序 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 SSO 的教程Amazon CLI和.NET 应用程序

本教程向您展示如何为基本.NET 应用程序和测试 SSO 用户启用 SSO。它使用Amazon CLI生成临时 SSO 令牌而不是以编程方式生成

本教程向您展示了 SSO 功能的一小部分Amazon SDK for .NET。有关将 IAM 身份中心与 IAM 身份中心一起使用的完整详细信息Amazon SDK for .NET,请参阅以下主题背景信息。在该主题中,请特别参见名为的小节中对此场景的高级描述Amazon CLI和.NET 应用程序

注意

本教程中的几个步骤可帮助您配置服务,例如Amazon Organizations和 IAM 身份中心。如果您已经执行了这些配置,或者您只对代码感兴趣,则可以跳到带有以下内容的部分示例代码

先决条件

  • 如果您尚未配置开发环境,请进行配置。这在以下各节中进行了描述安装和配置工具链设置您的项目

  • 识别或创建至少一个Amazon Web Services 账户你可以用它来测试 SSO。就本教程而言,这称为测试Amazon Web Services 账户或者干脆地测试账户

  • 识别一个SSO 用户谁能为你测试 SSO。此人将使用 SSO 和您创建的基本应用程序。在本教程中,该人可能是您(开发人员)或其他人。我们还建议使用 SSO 用户在不在您的开发环境中的计算机上工作的设置。但是,这并不是绝对必要的。

  • SSO 用户的计算机必须安装与您用来设置开发环境的.NET 框架兼容。

  • 确保Amazon CLI版本 2 是已安装在 SSO 用户的计算机上。你可以通过运行来检查这一点aws --version在命令提示符或终端中。

设置 Amazon

本节向您展示如何设置各种Amazon本教程的服务。

要执行此设置,请先登录测试Amazon Web Services 账户作为管理员。然后,执行以下操作:

Amazon S3

转到亚马逊 S3 控制台并添加一些无害的桶。在本教程的稍后部分,SSO 用户将检索这些存储桶的列表。

Amazon我是

转到IAM 控制台并添加一些 IAM 用户。如果您向 IAM 用户授予权限,请将权限限制为几个无害的只读权限。在本教程的稍后部分,SSO 用户将检索这些 IAM 用户的列表。

Amazon Organizations

转到Amazon Organizations控制台并启用 “组织”。有关更多信息,请参阅 Amazon Organizations 用户指南中的创建企业

此操作添加了测试Amazon Web Services 账户将组织视为管理账户。如果您有其他测试账户,则可以邀请他们加入组织,但本教程没有必要这样做。

IAM Identity Center

转到IAM 身份中心控制台并启用 SSO。如有必要,请进行电子邮件验证。有关更多信息,请参见启用 IAM 身份中心IAM 身份中心用户指南

然后,执行以下配置。

  1. 转到设置页面。寻找“访问门户 URL”并记录该值以备日后使用sso_start_url设置。

  2. 在旗帜里Amazon Web Services Management Console,寻找Amazon Web Services 区域这是在您启用 SSO 时设置的。这是左侧的下拉菜单Amazon Web Services 账户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. 转到Amazon账户页面并选择Amazon您之前添加到组织中的帐户。

  7. 概述该页面的部分,找到账号 ID并将其记录下来以备日后使用sso_account_id设置。

  8. 选择用户和群组选项卡,然后选择分配用户或群组

  9. 分配用户和群组页面,选择群组选项卡,选择您之前创建的群组,然后选择下一步

  10. 选择您之前创建的权限集,然后选择下一步,然后选择提交。配置需要几分钟。

创建示例应用程序

创建以下应用程序。它们将在 SSO 用户的计算机上运行。

包括NuGet包AWSSDK.SSOAWSSDK.SSOOIDC除了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; } } }

包括NuGet包AWSSDK.SSOAWSSDK.SSOOIDC除了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 用户的列表外,这些应用程序还显示启用 SSO 的配置文件的用户身份 ARN,即my-sso-profile在本教程中。

指导 SSO 用户

要求 SSO 用户查看他们的电子邮件并接受 SSO 邀请。系统会提示他们设置密码。该邮件可能需要几分钟才能到达 SSO 用户的收件箱。

向 SSO 用户提供您之前创建的应用程序。

然后,让 SSO 用户执行以下操作:

  1. 如果包含共享的文件夹Amazon config文件不存在,请创建它。如果该文件夹确实存在并且有一个名为的子文件夹.sso,删除该子文件夹。

    此文件夹的位置通常为%USERPROFILE%\.aws在 Windows 和~/.aws在 Linux 和 macOS 中。

  2. 创建共享Amazon 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. 运行亚马逊 S3 应用程序。出现运行时异常。

  4. 运行以下 Amazon CLI 命令:

    aws sso login --profile my-sso-profile
  5. 在生成的 Web 登录页面中,登录。使用邀请消息中的用户名和为响应该消息而创建的密码。

  6. 再次运行 Amazon S3 应用程序。该应用程序现在显示 S3 存储段的列表。

  7. 运行 IAM 应用程序。该应用程序显示 IAM 用户的列表。即使没有进行第二次登录,也是如此。IAM 应用程序使用先前创建的临时令牌。

清除

如果您不想保留在本教程中创建的资源,请清理它们。这些可能是Amazon开发环境中的资源或资源,例如文件和文件夹。