How to delete Identity User in Asp.net core ? 2019

Asp.net-core By Sagar Jaybhay

Create Identity User: https://sagarjaybhay.com/identity-in-aspnet-core-part-1-sagar-jaybhay/

To delete identity users from a database we need to use a Post request call. This can be done using get call also but it is not recommended.

List Identity users in asp.net core
List Identity users in asp.net core

In the previous article, we have created ListOfUsers functionality where we added Edit User and Delete User buttons to a list of users. On delete button click we called DeleteUser method in our RoleManage Controller and it is a post method.

For button click which is post-call, we create a form tag where we add button and whose type is submitted and for action call, we give asp-action and asp-controller values respectively.  Below is code snippet for this

<div class="card-footer">
                <div class="row col-12">
                    
                    <a class="btn btn-primary " asp-route-id="@users.Id" asp-controller="Rolemanag" asp-action="EditUser"> Edit </a>
                    <div class="col-1"></div>
                    <form method="post" asp-action="DeleteUser" asp-controller="Rolemanag" asp-route-UserID="@users.Id">
                        <button type="submit" class="btn btn-danger " value="Delete">Delete</button>
                    </form>    

                </div>

In this pass asp-route-UserId for identifying the user by using userid using UserManager service which is injected in our controller.

       [HttpPost]
        public async Task<IActionResult> DeleteUser(string UserID)
        {

            var user =await _userManager.FindByIdAsync(UserID);
            if (user != null)
            {
                var result = await _userManager.DeleteAsync(user);

                if (result.Succeeded)
                    return RedirectToAction("ListOfRoles");

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("",error.Description);
                }
            }
            else
            {
                return View("NotFound");
            }

            return View("NotFound");
        }

In this method we first find a user by id if this user is found then we can delete else we return the result, NotFound Page.

Now this regular code, but if we have to delete something we need to add user confirmation for that. So there are 2 ways we can user confirmation for delete identity users in asp .net core.

First Approach:

In this approach, we have created an alert confirmation box. In this, we have a javascript function we added which returns a response that is true or false. And this is return response to form and if our response is true then our post method is called else it is not called.

Below is the code for onclick.

<form method="post" asp-action="DeleteUser" asp-controller="Rolemanag" asp-route-UserID="@users.Id">
                        <button type="submit" class="btn btn-danger " value="Delete" onclick="return confirm('Are you sure,for delete this user :@users.UserName')">Delete</button>
                    </form>

onclick in identity user in asp.net core
onclick in identity user in asp.net core

delete identity user in asp.net core
delete identity user in asp.net core

Second Approach To Delete Identity User

In this approach, we have created InLine alert because as in the previous approach we create alert but always it is not a good way to show the alert.

Inline Message Delete In Asp.Net Core
Inline Message Delete In Asp.Net Core

To achieve this functionality we have created one span and in that span element, we added text and two buttons which is for yes and No like below.

<form method="post" asp-action="DeleteUser" asp-controller="Rolemanag" asp-route-UserID="@users.Id">                        
                        
                        <span style="display: none" id="DeleteSpan_@users.Id">
                            
                            <span>Are you sure,for delete this user.</span>
                            <button type="submit" class="btn btn-danger" >Yes</button>
                            <a href="#" class="btn btn-primary" onclick="DeleteUser('@users.Id',false)">No</a>
                        </span>
                        <a href="#" class="btn btn-danger" value="Delete" onclick="DeleteUser('@users.Id',true)" id="DeleteButton">Delete</a>
                    </form>   

After this, In the above code, we have a delete button, when you click that delete button in-line message show and when you click on yes it will call our delete method in controller else for no button it reset the UI. Also for toggle view functionality, we created on a javascript function whose code like below

<script>

    function DeleteUser(UserID,isDeleted) {

        var ID = "DeleteSpan_" + UserID;
        if (!isDeleted) {
            document.getElementById('DeleteButton').style.display = "block";
            document.getElementById(ID).style.display = "none";
        } else {
            document.getElementById('DeleteButton').style.display = "none";
                document.getElementById(ID).style.display = "block";
        }
    }

</script>

Inline Mesasge asp.net core part 2
Inline Mesasge asp.net core part 2

GitHub Project Link: https://github.com/Sagar-Jaybhay/LearnAspNetCore

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts