关于使用新的 Asp.net 身份安全框架的文档非常少。
我已经拼凑了我可以尝试创建一个新角色并向其中添加一个用户的内容。我尝试了以下方法:在 ASP.NET Identity 中添加角色 https://stackoverflow.com/questions/19541101/add-role-in-asp-net-identity
看起来它可能已经从这个博客中获取了信息:使用 ASP.NET 身份构建一个简单的待办事项应用程序并将用户与待办事项关联起来 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
我已将代码添加到数据库初始化程序中,该初始化程序在模型更改时运行。它失败了RoleExists
函数出现以下错误:
System.InvalidOperationException
发生在 mscorlib.dll 中
实体类型 IdentityRole 不是当前上下文模型的一部分。
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));
}
}
任何帮助表示赞赏。
开始了:
var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
if(!roleManager.RoleExists("ROLE NAME"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "ROLE NAME";
roleManager.Create(role);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)