【发布时间】:2014-07-21 12:54:51
【问题描述】:
我正在关注这篇文章以删除 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
{
return View();
}
}
我在任何地方都找不到IdentityManager,而且context.Users 也没有FindAsync() 方法。
如何在 Identity 2.0 中正确删除用户及其相关记录?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-5 asp.net-identity