我正在尝试绘制以下内容: 一个用户来 许多团体在哪里 每个用户都具有该组的特定角色
e.g。 用户1:蒂姆 蒂姆是A,B,C组的成员 他有 角色:A组的管理员 职责:B组成员 角色:C组的访客
现在这是我目前的代码:
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using MapGroupsToRoles.Models;
using System.Data.Entity;
namespace MapGroupsToRoles.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public ApplicationUser() { this.RolesToGroups = new List<GroupToRoles>(); }
public ICollection<GroupToRoles> RolesToGroups { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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 GroupToRoles
{
public int GroupToRolesId { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Group Group { get; set; }
public ClanRole Role { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public System.Data.Entity.DbSet<GroupToRoles> GroupToRolesTable { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
modelbuilder.Entity<ApplicationUser>().ToTable("Users");
// modelbuilder.Entity<ApplicationUser>().Ignore(u => u.Id);
modelbuilder.Entity<ApplicationUser>().HasKey(u => u.Id);
modelbuilder.Entity<ApplicationUser>().HasMany(u => u.RolesToGroups).WithRequired().WillCascadeOnDelete(true);
modelbuilder.Entity<GroupToRoles>().ToTable("GroupToRolesTable").HasKey(u => u.GroupToRolesId);
modelbuilder.Entity<IdentityUserLogin>().HasKey(u => u.ProviderKey);
modelbuilder.Entity<IdentityUserRole>().HasKey(u => u.RoleId);
modelbuilder.Entity<Group>().ToTable("Groups");
modelbuilder.Entity<Group>().HasKey(u => u.GroupId);
modelbuilder.Entity<Group>().HasMany(u => u.Roles).WithRequired().WillCascadeOnDelete(true);
}
}
}
现在我的模特:
namespace MapGroupsToRoles.Models
{
public class ClanRole
{
public string RoleName { get; set; }
}
}
namespace MapGroupsToRoles.Models
{
public class Group
{
public int GroupId { get; set; }
public Group() { }
public string GroupName { get; set; }
public ICollection<GroupToRoles> Roles { get; set; }
}
}
我已经设法让一些工作,但是如果你将它粘贴到一个带有Identity模板的默认MVC5项目中,你应该注意到:
还有其他问题,但那些问题很重要
作为最后一点,使用ClanRoles而不是标准身份角色的原因是因为我需要两组角色 - 一组用于组(或者你可能猜到的部族)和一组用于应用程序。如果有一种方法可以分开(我还没有考虑过),请告诉我
非常感谢任何帮助或指导