【问题标题】:What's the best way to perform CRUD with ASP.NET Identity?使用 ASP.NET Identity 执行 CRUD 的最佳方法是什么?
【发布时间】:2015-05-29 16:55:54
【问题描述】:
默认情况下,用于身份验证的 ASP.NET MVC 的 Identity 实现不支持对其用户的完整 CRUD 操作。
我已经知道了has:
注册、登录、管理和更改密码
但问题是doesn't have:
帐户更新或编辑、选择用户角色和删除用户
我认为Identity 仍然不完整,因为没有简明的文档,这导致我的学习曲线非常难看。或者如果有更好的学习方法和地点,你能指导我吗?非常感谢!
【问题讨论】:
标签:
asp.net-mvc-5
asp.net-identity
【解决方案1】:
只是一些可用方法的简单示例。
用户
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();
希望这会有所帮助,身份很棒!