[英]How to delete users that were created with UserManager.CreateAsync
Using asp.net mvc5, my user management systems seems to work. 使用asp.net mvc5,我的用户管理系统似乎工作。 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(). 我阅读的大多数地方,都在谈论使用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)
(给出其名称或用户ID)
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?
但这肯定不适合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);
从Microsoft.AspNet.Identity版本2.0.0.0开始,您现在可以使用
UserManager.Delete(user);
删除具有Identity的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).
较新版本的ASP.NET支持Identity和Membership,而Identity是默认设置,而旧版本仅支持Membership(在这两个身份验证系统中)。
When you create a user with UserManager.CreateAsync
, you are doing so within the Microsoft.AspNet.Identity
namespace. 使用
UserManager.CreateAsync
创建用户时,您在Microsoft.AspNet.Identity
命名空间中执行此操作。 When you are attempting to delete a user with Membership.DeleteUser
, you are doing so within the System.Web.Security
namespace. 当您尝试使用
Membership.DeleteUser
删除用户时,您在System.Web.Security
命名空间中执行此操作。 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 . 正如另一条评论所提到的, Identity尚未支持删除用户 ,但它是2014年春季发布路线图中的第一个项目 。
But why wait? 但为什么要等? Add another property to the ApplicationUser model like this:
将另一个属性添加到ApplicationUser模型,如下所示:
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. 当删除的用户尝试重新注册而不是
UserManager.Create
,请在注册页面上使用UserManager.Update
及其新信息。
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 .
如果您确实必须从数据库中清除其信息,则可以使用Entity Framework直接执行此操作 。
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 因为你的数据类型是一个字符串,而不是一个布尔值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.