Delete User Identity

Asked

Viewed 205 times

0

I’m new to the Identity, I’m having difficulty implementing the Delete Already Registered Users method. I’ve done Action, I ran View, but I have no knowledge of what methods Identity use to make such operation. I searched on Google, but I must be putting 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();
    }
}
  • If you can put an excerpt of code, the question got a little fuzzy. Identity is nothing more than the increment of a value, so I don’t know what this influences in deleting value, unless this value is key in another or more tables.

  • Sorry @Rafa_developer, it was a bit vague anyway. I’m using Identity for user authentication, the one that works with Claims , Roles... I’m doing the CRUD to delete the user, but I don’t know what Identity methods to use to do this. I’ve only done one Action so far. I’ll show you right below

  • @If you do not add answers, click on the button edit at the bottom left corner when you want to add something new to your question.

1 answer

1

Response based on this reply of OS:

In the code below is deleted all logins users of the roles 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 will probably have to make an adjustment or another in the code, but the way is this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.