// snippet-end:[STS.dotnetv3.AssumeRoleAsync] // snippet-start:[IAM.dotnetv3.DeleteResourcesAsync] /// <summary> /// Delete the user, and other resources created for this example. /// </summary> /// <param name="client">The initialized client object.</param> /// <param name=accessKeyId">The Id of the user's access key.</param>" /// <param name="userName">The user name of the user to delete.</param> /// <param name="policyName">The name of the policy to delete.</param> /// <param name="policyArn">The Amazon Resource Name ARN of the Policy to delete.</param> /// <param name="roleName">The name of the role that will be deleted.</param> public static async Task DeleteResourcesAsync( AmazonIdentityManagementServiceClient client, string accessKeyId, string userName, string policyArn, string roleName) { var detachPolicyResponse = await client.DetachRolePolicyAsync(new DetachRolePolicyRequest { PolicyArn = policyArn, RoleName = roleName, }); var delPolicyResponse = await client.DeletePolicyAsync(new DeletePolicyRequest { PolicyArn = policyArn, }); var delRoleResponse = await client.DeleteRoleAsync(new DeleteRoleRequest { RoleName = roleName, }); var delAccessKey = await client.DeleteAccessKeyAsync(new DeleteAccessKeyRequest { AccessKeyId = accessKeyId, UserName = userName, }); var delUserResponse = await client.DeleteUserAsync(new DeleteUserRequest { UserName = userName, }); }
public static void DeleteAccessKeysForUser(AmazonIdentityManagementServiceClient client, string username) { ListAccessKeysResponse response = client.ListAccessKeysAsync(new ListAccessKeysRequest() { UserName = username }).Result; foreach (AccessKeyMetadata akm in response.AccessKeyMetadata) { client.DeleteAccessKeyAsync(new DeleteAccessKeyRequest() { UserName = username, AccessKeyId = akm.AccessKeyId }).Wait(); } }
/// <summary> /// Deletes the User, Group, and AccessKey which were created for the purposes of /// this example. /// </summary> /// <param name="client">The IAM client used to delete the other /// resources.</param> /// <param name="userName">The name of the user that will be deleted.</param> /// <param name="groupName">The name of the group that will be deleted.</param> /// <param name="accessKeyId">The AccessKeyId that represents the /// AccessKey that was created for use with the ListBucketsAsync /// method.</param> public static async Task CleanUpResources(AmazonIdentityManagementServiceClient client, string userName, string groupName, string accessKeyId) { // Remove the user from the group. var removeUserRequest = new RemoveUserFromGroupRequest() { UserName = userName, GroupName = groupName, }; await client.RemoveUserFromGroupAsync(removeUserRequest); // Delete the client access keys before deleting the user. var deleteAccessKeyRequest = new DeleteAccessKeyRequest() { AccessKeyId = accessKeyId, UserName = userName, }; await client.DeleteAccessKeyAsync(deleteAccessKeyRequest); // Now we can safely delete the user. var deleteUserRequest = new DeleteUserRequest() { UserName = userName, }; await client.DeleteUserAsync(deleteUserRequest); // We have to delete the policy attached to the group first. var deleteGroupPolicyRequest = new DeleteGroupPolicyRequest() { GroupName = groupName, PolicyName = PolicyName, }; await client.DeleteGroupPolicyAsync(deleteGroupPolicyRequest); // Now delete the group. var deleteGroupRequest = new DeleteGroupRequest() { GroupName = groupName, }; await client.DeleteGroupAsync(deleteGroupRequest); Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------"); Console.WriteLine("Deleted the user and group created for this example."); }
/// <summary> /// Initializes an IAM Client Object and then deletes the IAM AccessKey. /// </summary> public static async Task Main() { string accessKeyId = "--AccessKeyID--"; string userName = "******"; var client = new AmazonIdentityManagementServiceClient(); var response = await client.DeleteAccessKeyAsync(new DeleteAccessKeyRequest { AccessKeyId = accessKeyId, UserName = userName, }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("Policy successfully deleted."); } else { Console.WriteLine("Could not delete policy."); } }