How to delete a user and its related data in ASP.NET MVC Identity 2.0

UserManager defines a number of inbuilt method like FindAsyncFindByIdAsyncFindByNameAsync  and FindByEmailAsync. These all can be used to retrieve and retrieve user details. UserManager also has DeleteAsync method which accepts a user object as a parameter and deletes the current user. To get the roles a user is the member of Identity gives UserManager has GetRolesAsync method where it accepts the ID of the user.

Also, UserManager.RemoveLoginAsync method removes a login from a user.

UserManager.RemoveFromRoleAsync: removes user from a Role

UserManager.FindAsync: Returns the user associated with this login.

UserManager.FindByIdAsync : Finds a user by ID.

UserManager.FindByNameAsync :  Finds a user by username.

UserManager.FindByEmailAsync : Finds a user by e-mail.


Now let’s create a method called DeleteUser in ManageUsers Controller that accepts UserId as a parameter.


The codes in above method is very simple and easy to understand.


Now let’s try deleting a user. 

For now, I have added a delete Action Button on UsersWithRoles views as discussed in the previous example. [http://localhost:64303/ManageUsers/UsersWithRoles].


You can find full working Source Code at https://github.com/nishanaryal/ASP.NET-MVC

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)