ASP.NET MVC 5如何删除用户和身份的2.0相关数据 [英] ASP.NET MVC 5 how to delete a user and its related data in Identity 2.0

查看:202
本文介绍了ASP.NET MVC 5如何删除用户和身份的2.0相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我下面这篇文章中标识2.0删除用户
<一href=\"http://www.asp.net/mvc/tutorials/mvc-5/introduction/examining-the-details-and-delete-methods\">http://www.asp.net/mvc/tutorials/mvc-5/introduction/examining-the-details-and-delete-methods

Hi I'm following this article to delete a user in Identity 2.0 http://www.asp.net/mvc/tutorials/mvc-5/introduction/examining-the-details-and-delete-methods

不过,我需要先删除AspNetUserRoles所有相关记录,然后删除该用户。

However, I need to delete all related records in AspNetUserRoles first and then delete the user.

我发现这是写在身份1.0和一些本示例内部使用的方法是不存在的,例如,

I found an example which is written in Identity 1.0 and some of methods used inside this example don't exist.

   // POST: /Users/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                var user = await context.Users.FindAsync(id);
                var logins = user.Logins;
                foreach (var login in logins)
                {
                    context.UserLogins.Remove(login);
                }
                var rolesForUser = await IdentityManager.Roles.GetRolesForUserAsync(id, CancellationToken.None);
                if (rolesForUser.Count() > 0)
                {

                    foreach (var item in rolesForUser)
                    {
                        var result = await IdentityManager.Roles.RemoveUserFromRoleAsync(user.Id, item.Id, CancellationToken.None);
                    }
                }
                context.Users.Remove(user);
                await context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

我找不到IdentityManager从任何地方,并且context.Users没有FindAsync()方法,无论是。

I cannot find "IdentityManager" from anywhere, and context.Users doesn't have FindAsync() method either.

请帮忙弄清楚如何正确删除用户和身份的2.0相关记录

Please help to figure out how to properly delete a User and its related records in Identity 2.0

感谢。

推荐答案

我认为你正在寻找的类是的的UserManager 和的 RoleManager 。在我看来,他们是更好的办法,而不是去反对直接的环境。

I think the classes you're looking for are the UserManager and the RoleManager. In my opinion they are the better way instead of going against the context directly.

的UserManager 定义的方法的 RemoveFromRoleAsync ,让你删除的用户从给定角色的能力(由他的主要标识)。它还定义了几个查找方法,例如 FindAsync FindByIdAsync ,的 FindByNameAsync ,或 FindByEmailAsync 。它们都可以被用于检索用户。要删除一个用户,你应该使用 DeleteAsync 其中接受用户对象作为参数的方法。为了得到角色的用户是身份的成员给你的 GetRolesAsync 方法,你的用户的ID通过。我也看到你想删除用户的登录。为此,你应该使用 RemoveLoginAsync 方法。

The UserManager defines a method RemoveFromRoleAsync which gives you the ability to remove the user (identified by his key) from a given role. It also defines several Find methods, such as FindAsync, FindByIdAsync, FindByNameAsync, or FindByEmailAsync. They all can be used to retrieve a user. 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. Also I see that you're trying to remove a login from a user. For this purpose you should use the RemoveLoginAsync method.

所有的一切你code将类似于以下之一:

All in all your code would look similar to the following one:

// POST: /Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{
  if (ModelState.IsValid)
  {
    if (id == null)
    {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var user = await _userManager.FindByIdAsync(id);
    var logins = user.Logins;
    var rolesForUser = await _userManager.GetRolesAsync(id);

    using (var transaction = context.Database.BeginTransaction())
    {
      foreach (var login in logins.ToList())
      {
        await _userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
      }

      if (rolesForUser.Count() > 0)
      {
        foreach (var item in rolesForUser.ToList())
        {
          // item should be the name of the role
          var result = await _userManager.RemoveFromRoleAsync(user.Id, item);
        }
      }

      await _userManager.DeleteAsync(user);
      transaction.commit();
    }

    return RedirectToAction("Index");
  }
  else
  {
    return View();
  }
}

您需要这个片段适应您的需求,因为我没有一个想法,您的 IdentityUser 执行的样子。记住申报的UserManager需要。一个例子,你可以怎么做这样可以在使用建立在Visual Studio中新的项目中找到个人帐户

You'll need to adjust this snippet to your needs, because I don't have an idea how your IdentityUser implementation looks like. Remember to declare the UserManager as needed. An example how you could do this can be found when you create a new project in Visual Studio using Individual Accounts.

这篇关于ASP.NET MVC 5如何删除用户和身份的2.0相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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