How to delete users that were created with UserManager.CreateAsync

走远了吗. 提交于 2019-12-04 03:16:02
jporcenaluk

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!