使用 ASP.NET Identity 执行 CRUD 的最佳方式是什么?
What's the best way to perform CRUD with ASP.NET Identity?
默认情况下,ASP.NET MVC 对 Identity
身份验证的实现不支持其用户的完整 CRUD
操作。
我已经知道了has
:
Register, Login, Manage, and Change Password
但问题是doesn't have
:
Account Update or Edit, Select User Role, and Delete User
我认为 Identity
仍然 不完整 在某种程度上没有简明的文档,这导致我的学习曲线看起来非常难看。或者如果有更好的学习方法和地方,可以指导一下吗?非常感谢!
只是一些可用方法的简单示例。
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();
希望这对您有所帮助,Identity 很棒!!
默认情况下,ASP.NET MVC 对 Identity
身份验证的实现不支持其用户的完整 CRUD
操作。
我已经知道了has
:
Register, Login, Manage, and Change Password
但问题是doesn't have
:
Account Update or Edit, Select User Role, and Delete User
我认为 Identity
仍然 不完整 在某种程度上没有简明的文档,这导致我的学习曲线看起来非常难看。或者如果有更好的学习方法和地方,可以指导一下吗?非常感谢!
只是一些可用方法的简单示例。
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();
希望这对您有所帮助,Identity 很棒!!