本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
适用于 的 教程IAM Identity Center仅使用 .NET 应用程序
本教程介绍如何启用Amazon IAM Identity Center (successor to Amazon Single Sign-On)适用于基本应用程序和测试 SSO 用户。它将应用程序配置为以编程方式生成临时 SSO 令牌,而不是使用Amazon CLI.
在开始本教程之前,请参阅背景信息用于使用IAM Identity Center使用Amazon SDK for .NET. 另请参阅名为的小节中此场景的高级描述.NET 应用程序.
本教程中的几个步骤可以帮助您配置服务,如Amazon Organizations和IAM Identity Center. 如果你已经执行了该配置,或者如果你只对该代码感兴趣,则可以跳至带示例代码.
先决条件
-
识别或创建至少一个Amazon Web Services 账户您可以用来测试IAM Identity Center. 在本教程中,这称为测试Amazon Web Services 账户或者简单地测试账户.
-
识别SSO 用户谁能测试IAM Identity Center对于你。这是将使用 SSO 和您创建的基本应用程序的人。在本教程中,那个人可能是你(开发人员)或其他人。我们还建议使用 SSO 用户在不在开发环境中的计算机上工作的设置。但是,这并非绝对必要。
-
SSO 用户的计算机必须安装与用于设置开发环境的 .NET 框架兼容。
设置 Amazon
本节介绍如何设置各种Amazon本教程的服务。
要执行此设置,请先登录测试Amazon Web Services 账户作为管理员。然后,执行以下操作:
Amazon S3
转至Amazon S3 控制台
AmazonIAM
转至IAM 控制台
Amazon Organizations
转至Amazon Organizations控制台
此操作添加了测试Amazon Web Services 账户将该组织作为管理账户. 如果您有其他测试帐户,则可以邀请他们加入组织,但是这样做对于本教程并不是必要的。
IAM Identity Center
转至Amazon IAM Identity Center (successor to Amazon Single Sign-On)控制台
然后,执行以下配置。
-
转至设置页. 查找“用户门户网址”并记录该值以供以后在
sso_start_url
设置。 -
在横幅中Amazon Web Services Management Console,查找Amazon Web Services 区域这是在启用时设置的IAM Identity Center. 这是在Amazon Web Services 账户ID。记录地区代码以供以后在
sso_region
设置。这个代码将类似于us-east-1
. -
按如下方式创建 SSO 用户:
-
转至用户页.
-
选择添加用户然后输入用户的用户名、电子邮件地址、名, 和姓. 然后选择下一步。
-
选择下一步在群组页面上,然后查看信息并选择添加用户.
-
-
按如下所示创建组:
-
转至Groups页.
-
选择创建组然后输入组的Group name和说明.
-
在将用户添加到组在部分中,选择您之前创建的测试 SSO 用户。然后,选择创建组。
-
-
创建权限集合,如下所示:
-
转至权限集页面然后选择创建权限集合.
-
Select创建自定义权限集合然后选择后续:Details (详细信息)。
-
在本教程中,输入
SSOReadOnlyRole
(对于 )名称然后输入说明. -
Select创建自定义权限策略并输入以下策略:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "iam:ListUsers" ], "Resource": "*" } ] }
-
选择 Next:。标签、后续:审核, 和Create.
-
在中记录权限集的名称,以供以后使用。
sso_role_name
设置。
-
-
转至Amazon账户页面然后选择Amazon您之前添加到组织的帐户。
-
在概述该页面的部分,找到账户 ID将它记录下来以供将来使用
sso_account_id
设置。 -
选择用户和组选项卡,然后选择分配用户或组.
-
在存储库的分配用户和组页面中,选择Groups选项卡中,选择您之前创建的组,然后选择下一步.
-
选择您之前创建的权限集,然后选择下一步,然后选择提交. 该配置需要花费一些时间。
创建例应用程序
创建以下应用程序。它们将在 SSO 用户的计算机上运行。
Include NuGet 包AWSSDK.SSO
和AWSSDK.SSOOIDC
此外AWSSDK.S3
和AWSSDK.SecurityToken
.
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; } } }
Include NuGet 包AWSSDK.SSO
和AWSSDK.SSOOIDC
此外AWSSDK.IdentityManagement
和AWSSDK.SecurityToken
.
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 用户
要求 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 -
运行 Amazon S3 应用程序。
-
在生成的 Web 登录页面中,登录。使用邀请消息中的用户名和为响应消息而创建的密码。
-
登录完成后,应用程序将显示 S3 存储桶列表。
-
运行 IAM 应用程序。应用程序将显示 IAM 用户的列表。即使没有执行第二次登录,情况也是如此。IAM 应用程序使用之前创建的临时令牌。
清除
如果您不想保留在本教程中创建的资源,请对其进行清理。这些可能是Amazon资源或开发环境中的资源,例如文件和文件夹。