在Asp.net身份创建角色MVC 5 [英] Creating Roles in Asp.net Identity MVC 5
问题描述
有即将使用新的Asp.net的身份安全框架很少的文档。
我是拼凑起来的东西我可以尝试创建一个新的角色和一个用户添加到它。我试过如下:在添加角色ASP.NET身份
这看起来可能得到从这个博客的信息:<一href=\"http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx\">building一个简单到做asp.net应用程序的身份和用户一起,并的关联
我已经加入了code到运行每当模型更改数据库初始化程序。它失败与以下错误 RoleExists
功能:
System.InvalidOperationException
出现在mscorlib.dll
实体类型IdentityRole是不是该机型为当前上下文的一部分。
块引用>保护覆盖无效种子(MyContext上下文)
{
VAR的UserManager =新的UserManager&LT; ApplicationUser&GT;(新UserStore&LT; ApplicationUser&GT;(上下文));
VAR RoleManager =新RoleManager&LT; IdentityRole&GT;(新Rolestore的&LT; IdentityRole&GT;(上下文)); //创建管理员角色
字符串角色名=管理员;
IdentityResult roleResult; //检查是否角色存在,如果不创建它
如果(!RoleManager.RoleExists(角色名))
{
roleResult = RoleManager.Create(新IdentityRole(角色名));
}
}任何帮助是AP preciated。
解决方案确认你有以下的
MyContext
类的签名
公共类MyContext:IdentityDbContext&LT;&MYUSER GT;
或者
公共类MyContext:IdentityDbContext
的的code是为我工作,没有任何修饰!的
There is very little documentation about using the new Asp.net Identity Security Framework.
I have pieced together what I could to try and create a new Role and add a User to it. I tried the following: Add role in ASP.NET Identity
which looks like it may have gotten the info from this blog: building a simple to-do application with asp.net identity and associating users with to-does
I have added the code to a Database Initializer that is run whenever the model changes. It fails on the
RoleExists
function with the following error:
System.InvalidOperationException
occurred in mscorlib.dll The entity type IdentityRole is not part of the model for the current context.
protected override void Seed (MyContext context) { var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); // Create Admin Role string roleName = "Admins"; IdentityResult roleResult; // Check to see if Role Exists, if not create it if (!RoleManager.RoleExists(roleName)) { roleResult = RoleManager.Create(new IdentityRole(roleName)); } }
Any help is appreciated.
解决方案Verify you have following signature of your
MyContext
class
public class MyContext : IdentityDbContext<MyUser>
Or
public class MyContext : IdentityDbContext
The code is working for me, without any modification!!!
这篇关于在Asp.net身份创建角色MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!