使用ASP.NET标识执行CRUD的最佳方法是什么?

时间:2015-05-29 02:33:59

标签: asp.net-mvc-5 asp.net-identity

默认情况下,ASP.NET MVC用于身份验证的Identity实现不支持对其用户进行完整的CRUD操作。

我已经知道has

  

注册,登录,管理和更改密码

但问题在于doesn't have

  

帐户更新或修改,选择用户角色和删除用户

我认为Identity仍然不完整,因为没有简明的文档会导致我看起来非常难看。或者,如果有更好的学习方式和地点,你能指导我吗?非常感谢你!

1 个答案:

答案 0 :(得分:5)

只是一些可用方法的快速示例。

  

用户

UserManager<ApplicationUser> userManager;
userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

var user = await userManager.FindByEmailAsync(Email);
await userManager.UpdateAsync(user);
await userManager.DeleteAsync(user);
  

角色

ApplicationDbContext context;
context = new ApplicationDbContext();

// Create Role
context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
{
    Name = RoleName
});
context.SaveChanges();

// Delete Role
var thisRole = context.Roles.Where(r => r.Name.Equals("Admin", StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
context.Roles.Remove(thisRole);
context.SaveChanges();

希望这有帮助,身份很棒!!