本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 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 CLI版本 2 是已安装在 SSO 用户的计算机上。你可以通过运行来检查这一点
aws --version
在命令提示符或终端中。
设置 Amazon
本节向您展示如何设置各种Amazon本教程的服务。
要执行此设置,请先登录测试Amazon Web Services 账户作为管理员。然后,执行以下操作:
Amazon S3
转到亚马逊 S3 控制台
Amazon我是
转到IAM 控制台
Amazon Organizations
转到Amazon Organizations控制台
此操作添加了测试Amazon Web Services 账户将组织视为管理账户。如果您有其他测试账户,则可以邀请他们加入组织,但本教程没有必要这样做。
IAM Identity Center
转到IAM 身份中心控制台
然后,执行以下配置。
-
转到设置页面。寻找“访问门户 URL”并记录该值以备日后使用
sso_start_url
设置。 -
在旗帜里Amazon Web Services Management Console,寻找Amazon Web Services 区域这是在您启用 SSO 时设置的。这是左侧的下拉菜单Amazon Web Services 账户ID。记录区域代码以供日后使用
sso_region
设置。这段代码将类似于us-east-1
。 -
按如下方式创建 SSO 用户:
-
转到用户页面。
-
选择添加用户然后输入用户的用户名,电子邮件地址,名字,以及姓氏。然后选择下一步。
-
选择下一步在群组页面上,然后查看信息并选择添加用户。
-
-
按如下方式创建群组:
-
转到群组页面。
-
选择创建群组然后输入群组的群组名称和描述。
-
在将用户添加到群组部分,选择您之前创建的测试 SSO 用户。然后,选择创建群组。
-
-
按如下方式创建权限集:
-
转到权限集页面并选择创建权限集。
-
下权限集类型,选择自定义权限集然后选择下一步。
-
打开内联政策并输入以下策略:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "iam:ListUsers" ], "Resource": "*" } ] }
-
在本教程中,输入
SSOReadOnlyRole
作为权限集名称。添加一个描述如果你愿意,然后选择下一步。 -
查看信息,然后选择创建。
-
记录权限集的名称以供日后使用
sso_role_name
设置。
-
-
转到Amazon账户页面并选择Amazon您之前添加到组织中的帐户。
-
在概述该页面的部分,找到账号 ID并将其记录下来以备日后使用
sso_account_id
设置。 -
选择用户和群组选项卡,然后选择分配用户或群组。
-
在分配用户和群组页面,选择群组选项卡,选择您之前创建的群组,然后选择下一步。
-
选择您之前创建的权限集,然后选择下一步,然后选择提交。配置需要几分钟。
创建示例应用程序
创建以下应用程序。它们将在 SSO 用户的计算机上运行。
包括NuGet包AWSSDK.SSO
和AWSSDK.SSOOIDC
除了AWSSDK.S3
和AWSSDK.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.SSO
和AWSSDK.SSOOIDC
除了AWSSDK.IdentityManagement
和AWSSDK.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 用户执行以下操作:
-
如果包含共享的文件夹Amazon
config
文件不存在,请创建它。如果该文件夹确实存在并且有一个名为的子文件夹.sso
,删除该子文件夹。此文件夹的位置通常为
%USERPROFILE%\.aws
在 Windows 和~/.aws
在 Linux 和 macOS 中。 -
创建共享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 -
运行亚马逊 S3 应用程序。出现运行时异常。
-
运行以下 Amazon CLI 命令:
aws sso login --profile my-sso-profile
-
在生成的 Web 登录页面中,登录。使用邀请消息中的用户名和为响应该消息而创建的密码。
-
再次运行 Amazon S3 应用程序。该应用程序现在显示 S3 存储段的列表。
-
运行 IAM 应用程序。该应用程序显示 IAM 用户的列表。即使没有进行第二次登录,也是如此。IAM 应用程序使用先前创建的临时令牌。
清除
如果您不想保留在本教程中创建的资源,请清理它们。这些可能是Amazon开发环境中的资源或资源,例如文件和文件夹。