asp Identity 2.0 adding new roles and adding user to roles

ⅰ亾dé卋堺 提交于 2019-12-03 11:59:24
Anthony Chu

System.Web.Security is the old ASP.NET Membership framework. ASP.NET Identity is in the namespace Microsoft.AspNet.Identity. Use a RoleManager to create roles, and a UserManager to add users to roles.

using (var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)) 
{
    roleManager.Create(new IdentityRole("Administrator"));
}

using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)))
{
    var user = new ApplicationUser { UserName = "admin" };
    userManager.Create(user, "admin321");
    userManager.AddToRole(user.Id, "Administrator");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!