Delete User Identity

0

I'm new to Identity , I'm having trouble implementing the Delete users already registered method. I've done Action, I've managed View, but I'm not aware of what methods Identity use to do such an operation. I researched Google, but I must be putting it wrong, because I have not found anything so far.

[HttpPost]
public ActionResult Delete(Guid id, IdentityUser user)
{
    try
    {
        _userManager.RemoveFromRoles(id = UserId);

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

I've only done Action so far:

public ActionResult Delete(Guid ID)
{
    return View(UserManager.FindById(ID));
}

[HttpPost]
public ActionResult Delete(Guid id, IdentityUser user)
{
    try
    {
        _userManager.RemoveFromRoles(id = UserId);
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
    
asked by anonymous 01.06.2018 / 05:57

1 answer

1

Response based on this response from the OS:

In the code below, all user's logins , role users and finally the user

// POST: /Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{
  if (ModelState.IsValid)
  {
    if (id == null)
    {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var user = await _userManager.FindByIdAsync(id);
    var logins = user.Logins;
    var rolesForUser = await _userManager.GetRolesAsync(id);

    using (var transaction = context.Database.BeginTransaction())
    {
      foreach (var login in logins.ToList())
      {
        await _userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
      }

      if (rolesForUser.Count() > 0)
      {
        foreach (var item in rolesForUser.ToList())
        {
          // item should be the name of the role
          var result = await _userManager.RemoveFromRoleAsync(user.Id, item);
        }
      }

      await _userManager.DeleteAsync(user);
      transaction.commit();
    }

    return RedirectToAction("Index");
  }
  else
  {
    return View();
  }
}

You'll probably have to make one adjustment or another in the code, but that's the way.

    
05.06.2018 / 16:11