简体   繁体   中英

How to delete users that were created with UserManager.CreateAsync

Using asp.net mvc5, my user management systems seems to work. I can login with google or with name/password..

but now I am working on a user management interface in which I need to be able to delete existing users. And this is starting to expose to me just how confusing the user management system is. There's so many different ways to deal with users.. and some of them don't work.

Most everywhere I read, it is talking about using the Membership.DeleteUser().

But that isn't working...

The users were created with.

var user = new ApplicationUser()
{
   UserName = model.UserName,
   Email = model.Email,
   ConfirmationToken = confirmationToken,
   IsConfirmed = false
};
var result = await UserManager.CreateAsync(user, model.Password);

Now later on.. how do I delete such a user? (given its name or userid)

I have tried what comes up most on various searches.. comes up with Membership as the solution. But this surely isn't right for MVC5? For example

var allusers = Membership.GetAllUsers();  // allusers is empty
bool success = Membership.DeleteUser(model.name); // <-- success = false

I can get all the users using this method..

ApplicationDbContext db = new ApplicationDbContext();
foreach (var user in db.Users) { ... }

And I can find an individual user with..

ApplicationDbContext db = new ApplicationDbContext();
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
ApplicationUser user = um.FindById(model.userId);

Now how do I delete one though? ....

Update

As of Microsoft.AspNet.Identity Version 2.0.0.0, you can now delete users with Identity using UserManager.Delete(user); .


For Posterity

You are referring to two different things, Identity and Membership. Newer versions of ASP.NET support Identity and Membership with Identity being the default, while older versions support only Membership (out of those two authentication systems).

When you create a user with UserManager.CreateAsync , you are doing so within the Microsoft.AspNet.Identity namespace. When you are attempting to delete a user with Membership.DeleteUser , you are doing so within the System.Web.Security namespace. They are living in two different worlds.

As another comment mentions, deleting users is not yet supported out of the box by Identity , but it is the first item on their roadmap for a Spring of 2014 release .

But why wait? Add another property to the ApplicationUser model like this:

public class ApplicationUser : IdentityUser
{
    public string IsActive { get; set; }
}

Then, in your controller for deleting a user:

user.IsActive = false;

Do a check when the user logs in:

if (user.IsActive == false)
{
    ModelState.AddModelError(String.Empty, "That user has been deleted.");
    return View(model);
}

When an deleted user attempts to re-register, instead of UserManager.Create , use UserManager.Update with their new information on the registration page.

These steps will effectively delete the user. If you truly must clear their information from your database, you can use Entity Framework to do that more directly .

added to the previous response. If you have

public class ApplicationUser : IdentityUser
{
    public string IsActive { get; set; }
}

Then, in your controller for deleting a user:

user.IsActive = false.ToString();

because your data type is a string and n ot a boolean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM