public static void CleanupTestRoleAndUser(string roleName, string userName) { using (var iamClient = new AmazonIdentityManagementServiceClient()) { try { var rolePolicies = iamClient.ListRolePolicies(new ListRolePoliciesRequest { RoleName = roleName }).PolicyNames; foreach (var policy in rolePolicies) { iamClient.DeleteRolePolicy(new DeleteRolePolicyRequest { RoleName = roleName, PolicyName = policy }); } iamClient.DeleteRole(new DeleteRoleRequest { RoleName = roleName }); } catch (NoSuchEntityException) { //this is ok - only clean up if it exists } try { var userPolicies = iamClient.ListUserPolicies(new ListUserPoliciesRequest { UserName = userName }).PolicyNames; foreach (var policy in userPolicies) { iamClient.DeleteUserPolicy(new DeleteUserPolicyRequest { UserName = userName, PolicyName = policy }); } var accessKeys = iamClient.ListAccessKeys(new ListAccessKeysRequest { UserName = userName }); foreach (var accessKey in accessKeys.AccessKeyMetadata) { iamClient.DeleteAccessKey(new DeleteAccessKeyRequest { UserName = userName, AccessKeyId = accessKey.AccessKeyId }); } iamClient.DeleteUser(new DeleteUserRequest { UserName = userName }); } catch (NoSuchEntityException) { //this is ok - only clean up if it exists } } }
private static void DeleteAccessKey(String DelAccessKeyid, String User = "") { if (String.IsNullOrEmpty(Token)) { stsClient = new AmazonIdentityManagementServiceClient(AccessKeyId, SecretKey, iamconfig); } else { stsClient = new AmazonIdentityManagementServiceClient(AccessKeyId, SecretKey, Token, iamconfig); } try { DeleteAccessKeyRequest accesskeyReq = new DeleteAccessKeyRequest(DelAccessKeyid); if (!String.IsNullOrEmpty(User)) { accesskeyReq.UserName = User; } DeleteAccessKeyResponse response = stsClient.DeleteAccessKey(accesskeyReq); Console.WriteLine("Access Key deleted successfully."); } catch (Exception ex) { Console.WriteLine("Error occured while creating user. " + ex.ToString()); } }
public void IdentityManagementServiceDeleteAccessKey() { #region 61a785a7-d30a-415a-ae18-ab9236e56871 var client = new AmazonIdentityManagementServiceClient(); var response = client.DeleteAccessKey(new DeleteAccessKeyRequest { AccessKeyId = "AKIDPMS9RO4H3FEXAMPLE", UserName = "******" }); #endregion }
public static void DeleteAccessKeysForUser(AmazonIdentityManagementServiceClient client, string username) { ListAccessKeysResponse response = client.ListAccessKeys(new ListAccessKeysRequest() { UserName = username }); foreach (AccessKeyMetadata akm in response.AccessKeyMetadata) { client.DeleteAccessKey(new DeleteAccessKeyRequest() { UserName = username, AccessKeyId = akm.AccessKeyId }); } }
public static void DeleteUser(this AmazonIdentityManagementServiceClient client, string userName) { try { var userPolicies = client.ListUserPolicies(new ListUserPoliciesRequest { UserName = userName }).PolicyNames; foreach (var policy in userPolicies) { var request = new DeleteUserPolicyRequest { UserName = userName, PolicyName = policy }; IgnoringNoSuchEntity(() => { client.DeleteUserPolicy(request); }); } } catch (NoSuchEntityException) { return; } var keys = client.ListAccessKeys(new ListAccessKeysRequest { UserName = userName }); foreach (var key in keys.AccessKeyMetadata) { var request = new DeleteAccessKeyRequest { UserName = userName, AccessKeyId = key.AccessKeyId }; IgnoringNoSuchEntity(() => { client.DeleteAccessKey(request); }); } IgnoringNoSuchEntity(() => client.DeleteUser(new DeleteUserRequest { UserName = userName })); }
public static void DeleteAccessKeys() { // Delete all the access keys we created for the Managing IAM Access Keys examples. var iamClient = new AmazonIdentityManagementServiceClient(); var requestAccessKeys = new ListAccessKeysRequest { // Use the user we created in the CreateUser example UserName = "******" }; var responseAccessKeys = iamClient.ListAccessKeys(requestAccessKeys); Console.WriteLine(" Access keys:"); foreach (var accessKey in responseAccessKeys.AccessKeyMetadata) { Console.WriteLine(" {0}", accessKey.AccessKeyId); iamClient.DeleteAccessKey(new DeleteAccessKeyRequest { UserName = "******", AccessKeyId = accessKey.AccessKeyId }); Console.WriteLine("Access Key " + accessKey.AccessKeyId + " deleted"); } }