【发布时间】:2016-11-20 23:49:34
【问题描述】:
所以我遇到了身份和用户角色的问题。我继承了基类,然后添加了一些自定义字段。用户对象现在有一个人,其他两个类继承自(Applicant 和 Reviewer)。
我在重新排序模型构建器中的表映射、删除我的自定义继承类、强制延迟加载方面做了很多尝试。
对此的任何建议或帮助将不胜感激。
这是申请者的上下文。
public class ApplicantContext : IdentityDbContext<User>
{
public ApplicantContext()
: base("ApplicantDbConnection")
{
this.Configuration.LazyLoadingEnabled = true;
}
public DbSet<Person> People { get; set; }
public DbSet<Applicant> Graduates { get; set; }
public DbSet<Reviewer> Reviewers { get; set; }
//stop pluralising generated tables
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Role>().HasKey<string>(r => r.Id).ToTable("Roles");
modelBuilder.Entity<User>().HasRequired(i => i.Person).WithMany().HasForeignKey<int>(i => i.PersonID);
modelBuilder.Entity<User>().HasMany<UserRole>((User u) => u.UserRoles);
modelBuilder.Entity<UserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("UserRoles");
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<IdentityRole>()
.ToTable("Roles");
modelBuilder.Entity<IdentityUserRole>()
.ToTable("UserRoles");
modelBuilder.Entity<IdentityUserClaim>()
.ToTable("UserClaims");
modelBuilder.Entity<IdentityUserLogin>()
.ToTable("UserLogins");
}
}
数据库初始化程序。事情的数据库方面似乎一切都很好,但是当 我登录系统,登录成功,但是当它 重定向到主控制器索引,索引页面使用 [Authorize(Roles="Reviewer")] 这就是失败的地方。它说 用户不在该角色中,但是在数据库中,UserId 是配对的 使用 UserRoles 表中的 RoleID。因此用户的角色是 空。
public class DataInitialiser : CreateDatabaseIfNotExists<ApplicantContext>
{
protected override void Seed(ApplicantContext context)
{
var manager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
manager.Create(new IdentityRole("Reviewer"));
manager.Create(new IdentityRole("Applicant"));
ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<User>(context));
User user = new User
{
Person = new Reviewer
{
FirstName = "Grant",
MiddleNames = "Mark",
Surname = "Weatherston",
OfficeID = 1,
},
Email = "test@test.com",
UserName = "test@test.com",
PhoneNumber = "0123456789",
};
userManager.Create(user, "Password123");
userManager.AddToRole(user.Id, "Reviewer");
context.SaveChanges();
base.Seed(context);
}
}
继承自 IdentityRole 的自定义角色类。
public class Role : IdentityRole
{
public Role() { }
public Role(string name) :base(name)
{
}
}
自定义用户类继承自身份用户并添加属性Person。
public class User : IdentityUser
{
public User() { }
public int PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
public virtual ICollection<UserRole> UserRoles {get;set;}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
自定义用户角色类。
public class UserRole : IdentityUserRole
{
}
自定义角色管理器。
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(RoleStore<IdentityRole> roleStore)
: base(roleStore)
{
}
}
自定义用户管理器
public class ApplicationUserManager : UserManager<User>
{
public ApplicationUserManager(IUserStore<User> store)
: base(store)
{
}
}
【问题讨论】:
标签: c# asp.net-mvc-5 asp.net-identity roles user-roles