What's the best way to perform CRUD with ASP.N

2019-07-08 02:53发布

问题:

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