Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? ....

like image 490
Takhion Stream Avatar asked Feb 02 '14 03:02

Takhion Stream


People also ask

How to delete user in ASP net Core Identity?

To delete a user you should use the DeleteAsync method which accepts a user object as a parameter. To get the roles a user is member of Identity gives you the GetRolesAsync method where you pass in the ID of the user.

What does UserManager CreateAsync do?

CreateAsync(TUser, String)Creates the specified user in the backing store with given password, as an asynchronous operation.


2 Answers

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.

like image 146
jporcenaluk Avatar answered Oct 21 '22 01:10

jporcenaluk


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

like image 37
Sanvir Avatar answered Oct 21 '22 02:10

Sanvir