什么是与ASP.NET身份执行CRUD的最佳方式? [英] What's the best way to perform CRUD with ASP.NET Identity?
问题描述
在默认情况下,ASP.NET MVC的实施身份
认证不支持全 CRUD
操作为它的用户
我已经知道了的
:
注册,登录,管理和更改密码
块引用>但问题是,
没有
:
帐户更新或编辑,选择用户角色和删除用户
块引用>我觉得
身份
还是不完整的的方式,有它使我有一个非常难看的学习曲线的简明文档。或者,如果有一个更好的方式和地点,学习它,你能指导我吗?非常感谢你!解决方案一些是availalbe方法只是一个简单的例子。
用户
块引用>的UserManager< ApplicationUser>的UserManager;
的UserManager =新的UserManager&所述; ApplicationUser>(新UserStore&所述; ApplicationUser>(新ApplicationDbContext()));VAR用户=等待userManager.FindByEmailAsync(邮箱);
等待userManager.UpdateAsync(用户);
等待userManager.DeleteAsync(用户);
角色
块引用>ApplicationDbContext背景;
上下文=新ApplicationDbContext();//创建角色
context.Roles.Add(新Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
{
NAME = ROLENAME
});
context.SaveChanges();//删除角色
VAR thisRole = context.Roles.Where(R = GT; r.Name.Equals(管理,StringComparison.CurrentCultureIgnoreCase))FirstOrDefault()。
context.Roles.Remove(thisRole);
context.SaveChanges();希望这有助于,身份是伟大的!
By default, ASP.NET MVC's implementation of
Identity
for authentication does not support fullCRUD
operations for it's users.I know it already
has
:Register, Login, Manage, and Change Password
But the problem is that
doesn't have
:Account Update or Edit, Select User Role, and Delete User
I think that
Identity
is still incomplete in a way that there is no concise documentation which causes me to have a very ugly looking learning curve. Or if there's a better way and place to learn it, can you guide me there? Thank you very much!解决方案Just a quick example of some of the methods that are availalbe.
Users
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);
Roles
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();
Hope this helps, Identity is great!!
这篇关于什么是与ASP.NET身份执行CRUD的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!