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

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

仅使用 .NET 应用程序的 SSO 教程

本教程介绍如何为基本应用程序和测试 SSO 用户启用 SSO。它将应用程序配置为以编程方式生成临时 SSO 令牌,而不是使用 Amazon CLI

本教程向您展示了Amazon SDK for .NET中的一小部分 SSO 功能。有关将 IAM Identity Center 与Amazon SDK for .NET配合使用的完整详细信息,请参阅带有背景信息的主题。在该主题中,请特别参见名为仅限 .NET 应用程序的小节中对此场景的总体描述。

注意

本教程中的几个步骤有助于您配置 Amazon Organizations 和 IAM Identity Center 等服务。如果您已经执行了该配置,或者只对代码感兴趣,则可以跳到带有示例代码的部分。

先决条件

  • 请配置您的开发环境(如果尚未配置)。在安装和配置工具链开始使用之类的章节中对此进行了描述。

  • 确定或创建至少一个可用于测试 SSO 的 Amazon Web Services 账户。就本教程而言,名为测试 Amazon Web Services 账户或者简称为测试账户

  • 确定可以为您测试 SSO 的 SSO 用户。此人将使用 SSO 和您创建的基本应用程序。在本教程中,此人可能是您(开发人员)或其他人。我们还建议采用这样的设置:SSO 用户使用不在您开发环境中的计算机。但是,这并不是必要的。

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

设置 Amazon

这一部分演示了如何为本教程设置各种 Amazon 服务。

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

Amazon S3

前往 Amazon S3 控制台并添加一些无关紧要的桶。在本教程的后面部分,SSO 用户将检索这些桶的列表。

Amazon IAM

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

Amazon Organizations

进入 Amazon Organizations 控制台并启用 Organizations。有关更多信息,请参阅 Amazon Organizations 用户指南中的创建企业

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

IAM Identity Center

前往 IAM Identity Center 控制台并启用 SSO。如有必要,请执行电子邮件验证。有关更多信息,请参阅 IAM Identity Center 用户指南中的启用 IAM Identity Center

然后,执行以下配置。

  1. 转到设置页面。查找访问门户 URL 并记录该值供以后在 sso_start_url 设置中使用。

  2. 在 Amazon Web Services Management Console的横幅中,查找启用 SSO 时设置的 Amazon Web Services 区域。这是 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 用户的计算机上运行。

AWSSDK.S3AWSSDK.SecurityToken 之外,还包括 NuGet 程序包 AWSSDK.SSOAWSSDK.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; } } }

AWSSDK.IdentityManagementAWSSDK.SecurityToken 之外,还包括 NuGet 程序包 AWSSDK.SSOAWSSDK.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

这些应用程序通过在 SSOAWSCredentials 对象的 Options 属性中提供回调方法来执行 SSO 登录任务。

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. 运行 Amazon S3 应用程序。

  4. 在最终的 Web 登录页面中,登录。使用邀请消息中的用户名和为回复邮件而创建的密码。

  5. 登录完成后,应用程序将显示 S3 桶列表。

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

清除

如果您不想保留在本教程中创建的资源,请将其清理干净。这包括 Amazon 资源或开发环境中的资源,例如文件和文件夹。