What's the best way to perform CRUD with ASP.NET Identity?

柔情痞子 提交于 2019-12-11 02:28:55

问题


By default, ASP.NET MVC's implementation of Identity for authentication does not support full CRUD 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!


回答1:


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!!



来源:https://stackoverflow.com/questions/30520190/whats-the-best-way-to-perform-crud-with-asp-net-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!