ASP身份加入2.0新的角色和将用户添加到角色 [英] asp Identity 2.0 adding new roles and adding user to roles
问题描述
我无法理解新身份的东西。我想弄清楚如何添加新角色和用户添加到这些角色。我试图创建一个角色管理页面和用户管理页面,我的应用程序,需要能够做到这两件事情。每次我尝试将角色添加到这样一个用户:System.Web.Security.Roles.AddUserToRole(安迪,管理);我得到角色管理器功能尚未启用。我没有自定义角色提供程序我试图转向作用经理已启用,但只是得到一个错误约没有一个角色提供。我想通了身份这一切都建在了吗?有人可以帮我通过这个?
I'm having trouble understanding the new Identity stuff. I'm trying to figure out how to add new roles and add user's to those roles. I'm trying to create a role management page and a user management page for my application and need to be able to do those two things. Everytime i try to add a role to a user like this: System.Web.Security.Roles.AddUserToRole("Andy", "admin"); I get "The Role Manager feature has not been enabled." I don't have a custom role provider and i tried turning role manager to enabled but just got an error about not having a role provider. I figured Identity had all this built in already? Can somebody help me through this?
推荐答案
System.Web.Security
是旧的ASP.NET成员资格框架。 ASP.NET身份是命名空间中的 Microsoft.AspNet.Identity
。使用 RoleManager
以创建角色,和的UserManager
将用户添加到角色。
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.
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
roleManager.Create(new IdentityRole("Administrator"));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var user = new ApplicationUser { UserName = "admin" };
userManager.Create(user, "admin321");
userManager.AddToRole(user.Id, "Administrator");
这篇关于ASP身份加入2.0新的角色和将用户添加到角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!