How to delete user with UserManager in mvc5

2019-05-23 08:42发布

问题:

I'm using mvc5, and everything about user account management I do with UserManager. It works good with roles, claims, etc. But I didn't find how to delete user with UserManager. Is there a way to delete user with UserManager? I can create Database context with dbset and then delete it from this context, but I don't want create dbcontext, userclass, etc. for one delete method.

回答1:

Delete was not supported in UserManager in 1.0, but its supported in the upcoming 2.0 release, and in the current 2.0 nightly builds if you want to preview the changes early.



回答2:

I had issues with the above answer, though I was able to work out what's wrong. I kept getting a cascading error. Basically the user was being deleted without the role being deleted. DeleteAsync was not doing that for me (I have the latest build of Identity Framework). Ended up passing both the userid and role into my code, deleting the user from the role, then deleting the user. Seems to work fine.

[HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Delete(string id, string role)
    {
        // Check for for both ID and Role and exit if not found
        if (id == null || role == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        // Look for user in the UserStore
        var user = UserManager.Users.SingleOrDefault(u => u.Id == id);

        // If not found, exit
        if (user == null)
        {
            return HttpNotFound();
        }

        // Remove user from role first!
        var remFromRole = await UserManager.RemoveFromRoleAsync(id, role);

        // If successful
        if (remFromRole.Succeeded)
        {
            // Remove user from UserStore
            var results = await UserManager.DeleteAsync(user);

            // If successful
            if (results.Succeeded)
            {
                // Redirect to Users page
                return RedirectToAction("Index", "Users", new {area = "Dashboard"});
            }
            else
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        }
        else
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

    }


回答3:

Using the updated asp.net identity I have the following code:

public UserManagerController()
        : this(new UserManager<User>(new UserStore<User>(new ApplicationDbContext())))
    {
    }

    public UserManagerController(UserManager<User> userManager)
    {
        UserManager = userManager;
    }

    public UserManager<User> UserManager { get; private set; }

    public async Task<ActionResult> Delete (string id)
    {
        var userContext = new ApplicationDbContext();


        var user = UserManager.Users.SingleOrDefault(u => u.Id == id);

        var userStore = new UserStore<User>(userContext);

        await UserManager.DeleteAsync(user);

        // var userManager = new UserManager<User>(userStore);

        // await userManager.DeleteAsync(user);

        return RedirectToAction("Index");
    }

This one now deletes the user. It is also no need to delete from UserRoles table as that is taken care of by UserManager.DeleteAsync(user).

Hope this helps a few. I spent some time figuring out why I got some errors.

Trond