【发布时间】:2019-03-06 10:20:35
【问题描述】:
我觉得我错过了什么。 Authorize 本身有效,但使用角色无效。 cshtml-Code 包含razor 的东西,我不确定我是否可以与基于角色的授权结合使用。此外,IsInRole 总是返回 false。
在表AspNetUserRoles 的我的数据库中,角色实例具有正确的RoleId 和UserId。我在数据库的Seed 方法中将用户添加到角色中。
if (!userManager.IsInRole(adminUser.Id, "SystemAdministrator"))
userManager.AddToRole(adminUser.Id, "SystemAdministrator");
我是否需要将它们添加到其他位置,例如在某种角色管理器中?
以下是我认为启动中配置的相关部分:
app.CreatePerOwinContext<RoleManager<AppRole>>((options, context) =>
new RoleManager<AppRole>(
new RoleStore<AppRole>(context.Get<MyANTon.DataContext.AntContext>())));
也许,在这之后,我必须将用户添加到角色中?
编辑:我发现了一个可能的错误来源。我的db context 似乎不包括像AspNetUserRoles 甚至AspNetUsers 这样的身份表。我上周从 Forms-Authentication 迁移到 Identities,这可能是现在的问题。我是否必须相应地更改上下文?它继承自IdentityDbContext<AppUser>,这就是为什么我不能只添加AspUserstuff(因为它已经存在),但是当我在运行时查看上下文时,它不存在......
下一次编辑:我的角色经理缺少web.config。添加后,我的data context 想法似乎真的是真的。现在,抛出的错误是:'The entity type AppUser is not part of the model for the current context.'。我的上下文继承自IdentityDbContext<AppUser>,那为什么不包含AppUser?
上下文类:
public class AntContext : IdentityDbContext<AppUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<AntContext>(null);
modelBuilder.Entity<AppUser>().ToTable("AppUsers");
base.OnModelCreating(modelBuilder);
}
控制器中的UserManager构造函数调用:
private UserManager<AppUser> userManager = new UserManager<AppUser>(new UserStore<AppUser>());
【问题讨论】:
标签: c# entity-framework asp.net-mvc-5 asp.net-identity asp.net-roles