如何删除那些与UserManager.CreateAsync创建的用户 [英] How to delete users that were created with UserManager.CreateAsync

查看:756
本文介绍了如何删除那些与UserManager.CreateAsync创建的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用asp.net mvc5,我的用户管理系统,似乎工作。我可以用谷歌或名/密码登录。

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.

大多数我到处看,它是在谈论使用Membership.DeleteUser()。

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

但是,这是不工作...

But that isn't working...

在使用者与创建。

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

现在以后..我怎么删除这些用户? (鉴于其名或用户ID)

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

我曾尝试与会员会出现什么最对各种搜索..出来作为解决方案。但是,这肯定是不对的MVC5?
例如:

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) { ... }

和我能找到单个用户。

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

推荐答案

更新

由于Microsoft.AspNet.Identity版本2.0.0.0,您可以使用现在删除用户与身份 UserManager.Delete(用户);

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

为后人

您指的是两个不同的东西,身份和会员资格。新版本的ASP.NET支持身份和会员通过身份是默认值,而旧版本只支持会员(出这两个认证系统)的。

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

当您创建 UserManager.CreateAsync 用户,你在这样做的 Microsoft.AspNet.Identity 命名空间。当你试图删除与 Membership.DeleteUser 用户,你在这样做的 System.Web.Security 命名空间。他们是生活在两个不同的世界。

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.

另一个注解中提到的,<一个href=\"http://stackoverflow.com/questions/19466222/whys-delete-not-supported-in-asp-net-identity\">deleting用户还没有被支持了由身份的方块,但它是第一在他们的路线图2014年发布的春天项目。

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.

但为什么还要等待?另一个属性添加到ApplicationUser模式是这样的:

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);
}

当删除用户尝试,而不是 UserManager.Create 来重新注册,使用 UserManager.Update 与在注册页面上的新的信息。

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

这些步骤的有效的删除用户。如果你真的必须从数据库中清除他们的信息,<一个href=\"http://stackoverflow.com/questions/20812473/how-can-i-use-entity-framework-6-and-my-repository-to-delete-multiple-records\">you可以使用实体框架做更直接的。

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.

这篇关于如何删除那些与UserManager.CreateAsync创建的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆