删除 IAM 用户 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

是否要将 .NET 应用程序部署到Amazon通过几个简单点击即可? 尝试我们的新.NET CLI 工具为了简化部署体验!阅读我们的博客帖子然后提交反馈GitHub

有关更多信息,请参阅部署工具

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

删除 IAM 用户

此示例向您演示如何使用Amazon SDK for .NET删除 IAM 用户。它首先删除访问密钥、附加的策略等资源,然后删除用户。

以下各节提供了此示例的片段。这些区域有:例子的完整代码在此之后显示,并且可以按原样构建和运行。

从用户中删除物品

以下代码段显示了在删除用户之前必须从用户中删除的项目的示例,例如托管策略和访问密钥。

这个示例在本主题末尾的显示了这个片段正在使用中。

// // Method to detach managed policies from a user private static async Task DetachPolicies( IAmazonIdentityManagementService iamClient, string userName) { ListAttachedUserPoliciesResponse responseManagedPolicies = await iamClient.ListAttachedUserPoliciesAsync( new ListAttachedUserPoliciesRequest{UserName = userName}); foreach(AttachedPolicyType policy in responseManagedPolicies.AttachedPolicies) { Console.WriteLine($"\tDetaching policy {policy.PolicyName}"); await iamClient.DetachUserPolicyAsync(new DetachUserPolicyRequest{ PolicyArn = policy.PolicyArn, UserName = userName}); } } // // Method to delete access keys from a user private static async Task DeleteAccessKeys( IAmazonIdentityManagementService iamClient, string userName) { ListAccessKeysResponse responseAccessKeys = await iamClient.ListAccessKeysAsync( new ListAccessKeysRequest{UserName = userName}); foreach(AccessKeyMetadata accessKey in responseAccessKeys.AccessKeyMetadata) { Console.WriteLine($"\tDeleting Access key {accessKey.AccessKeyId}"); await iamClient.DeleteAccessKeyAsync(new DeleteAccessKeyRequest{ UserName = userName, AccessKeyId = accessKey.AccessKeyId}); } }

删除该用户

以下代码段调用方法从用户中删除项目,然后删除该用户。

这个示例在本主题末尾的显示了这个片段正在使用中。

// // Method to delete a user private static async Task DeleteUser( IAmazonIdentityManagementService iamClient, string userName) { Console.WriteLine($"\nDeleting user {userName}..."); // // Remove items from the user // // Detach any managed policies await DetachPolicies(iamClient, userName); // Delete any access keys await DeleteAccessKeys(iamClient, userName); // DeleteLoginProfileAsycn(), DeleteUserPolicyAsync(), etc. // See the description of DeleteUserAsync for a full list. // // Delete the user // await iamClient.DeleteUserAsync(new DeleteUserRequest(userName)); Console.WriteLine("Done"); }

代码完成

此部分显示此示例的相关参考文献和完整代码。

using System; using System.Threading.Tasks; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; namespace IamDeleteUser { class Program { static async Task Main(string[] args) { if(args.Length != 1) { Console.WriteLine("\nUsage: IamDeleteUser user-name"); Console.WriteLine(" user-name - The name of the user you want to delete."); return; } // Create an IAM service client var iamClient = new AmazonIdentityManagementServiceClient(); // Delete the given user await DeleteUser(iamClient, args[0]); // Could display a list of the users that are left. } // // Method to delete a user private static async Task DeleteUser( IAmazonIdentityManagementService iamClient, string userName) { Console.WriteLine($"\nDeleting user {userName}..."); // // Remove items from the user // // Detach any managed policies await DetachPolicies(iamClient, userName); // Delete any access keys await DeleteAccessKeys(iamClient, userName); // DeleteLoginProfileAsycn(), DeleteUserPolicyAsync(), etc. // See the description of DeleteUserAsync for a full list. // // Delete the user // await iamClient.DeleteUserAsync(new DeleteUserRequest(userName)); Console.WriteLine("Done"); } // // Method to detach managed policies from a user private static async Task DetachPolicies( IAmazonIdentityManagementService iamClient, string userName) { ListAttachedUserPoliciesResponse responseManagedPolicies = await iamClient.ListAttachedUserPoliciesAsync( new ListAttachedUserPoliciesRequest{UserName = userName}); foreach(AttachedPolicyType policy in responseManagedPolicies.AttachedPolicies) { Console.WriteLine($"\tDetaching policy {policy.PolicyName}"); await iamClient.DetachUserPolicyAsync(new DetachUserPolicyRequest{ PolicyArn = policy.PolicyArn, UserName = userName}); } } // // Method to delete access keys from a user private static async Task DeleteAccessKeys( IAmazonIdentityManagementService iamClient, string userName) { ListAccessKeysResponse responseAccessKeys = await iamClient.ListAccessKeysAsync( new ListAccessKeysRequest{UserName = userName}); foreach(AccessKeyMetadata accessKey in responseAccessKeys.AccessKeyMetadata) { Console.WriteLine($"\tDeleting Access key {accessKey.AccessKeyId}"); await iamClient.DeleteAccessKeyAsync(new DeleteAccessKeyRequest{ UserName = userName, AccessKeyId = accessKey.AccessKeyId}); } } } }

其他注意事项

  • 有关必须从用户中删除的资源的信息,请参阅删除 UserAsync方法,但请务必使用引用方法的 Async 版本。