相关疑难解决方法(0)

ASP.NET MVC 5如何在Identity 2.0中删除用户及其相关数据

嗨,我正在关注本文以删除Identity 2.0中的用户 http://www.asp.net/mvc/tutorials/mvc-5/introduction/examining-the-details-and-delete-methods

但是,我需要先删除AspNetUserRoles中的所有相关记录,然后删除该用户.

我找到了一个用Identity 1.0编写的例子,这个例子中使用的一些方法不存在.

   // 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 …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-5 asp.net-identity

32
推荐指数
1
解决办法
4万
查看次数