ASP.NET MVC-5中的多个登录实体和多级访问权限

本文关键字:实体 多级 访问 权限 访问权 登录 MVC-5 NET ASP | 更新日期: 2023-09-27 18:21:31

我正在构建一个具有4级登录的系统,涉及4个实体。

  1. 系统所有者
  2. 分配器
  3. 代理人
  4. 子代理

系统所有者可以创建分发服务器登录、分发服务器创建代理登录和代理创建子代理登录。

我创建了4个项目,每个项目都有自己的登录标识。但问题是,我无法从系统所有者项目创建分发服务器登录详细信息。我需要将所有项目合并到1中吗?如果是,我如何为每个实体分离这些登录?

任何帮助都将不胜感激

ASP.NET MVC-5中的多个登录实体和多级访问权限

您不需要为每个用户创建四个独立的项目。您可以在单个项目中使用进行此操作。

您可以使用Microsoft ASP.Net Identity 2管理每种用户类型的页面和功能。

我可以解释这样做的基本想法。

步骤1

为每个用户创建四个角色

步骤2

为所有用户创建相同的登录页面。

步骤3

当用户登录时,识别他/她的角色在我们的系统中。

步骤4

根据登录用户的角色重定向到主页。

步骤5

为每个用户动态生成菜单。

步骤6

根据登录用户的角色对每个功能进行身份验证。

就是这样!!!

我创建了5个项目

  1. 所有实体的类库(包括4个类登录实体、SignInManager、用户管理器和DbContext)
  2. 系统所有者项目
  3. 分销商项目
  4. 代理商项目
  5. 子代理项目

注:我以这种方式设计类库,以确保我们可以使用迁移

我使用的是Microsoft.AspNet.Identity版本2。IdentityDbContext.ValidateEntity方法存在错误,它将检查表AspNetUsers中UserName的唯一规则。(因为当从IdentityUser继承的不同EntitySet之间存在相同的用户名或电子邮件时,根据AspNetUsers表进行检查会导致错误)

最后,我直接从DbContext类继承,并将Identity类放在那里。同时删除那里的ValidateEntity方法

这是我的上下文类代码:

public partial class MyDbContext: DbContext
{
    public MyDbContext() : base("name=MyDbContext") { }
    public static MyDbContext Create()
    {
        return new MyDbContext();
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        // Needed to ensure subclasses share the same table
        var user = modelBuilder.Entity<IdentityUser>()
            .ToTable("Users");
        user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
        user.HasMany(u => u.Claims).WithRequired().HasForeignKey(uc => uc.UserId);
        user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ul => ul.UserId);
        user.Property(u => u.UserName)
            .IsRequired()
            .HasMaxLength(256)
            .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UserNameIndex") { IsUnique = false })); // Unique False because there might have same UserName between EntitySet
        // CONSIDER: u.Email is Required if set on options?
        user.Property(u => u.Email).HasMaxLength(256);
        modelBuilder.Entity<IdentityUserRole>()
            .HasKey(r => new { r.UserId, r.RoleId })
            .ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserLogin>()
            .HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
            .ToTable("UserLogins");
        modelBuilder.Entity<IdentityUserClaim>()
            .ToTable("UserClaims");
        var role = modelBuilder.Entity<IdentityRole>()
            .ToTable("Roles");
        role.Property(r => r.Name)
            .IsRequired()
            .HasMaxLength(256)
            .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("RoleNameIndex") { IsUnique = false }));
        role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId);
        modelBuilder.Entity<SystemOwnerApplicationUser>().ToTable("SystemOwnerApplicationUsers");
        modelBuilder.Entity<DistributorApplicationUser>().ToTable("DistributorApplicationUsers");
        modelBuilder.Entity<AgentApplicationUser>().ToTable("AgentApplicationUsers");
        modelBuilder.Entity<SubagentApplicationUser>().ToTable("SubagentApplicationUsers");
    }
    public virtual DbSet<SystemOwnerApplicationUser> SystemOwnerApplicationUsers { get; set; }
    public virtual DbSet<DistributorApplicationUser> DistributorApplicationUsers { get; set; }
    public virtual DbSet<AgentApplicationUser> AgentApplicationUsers { get; set; }
    public virtual DbSet<SubagentApplicationUser> SubagentApplicationUsers { get; set; }
}

在项目之间创建用户可以通过这种方式完成

            if (!(context.DistributorApplicationUsers.Any(u => u.UserName == userName)))
            {
                var userStore = new UserStore<DistributorApplicationUser>(context);
                var userManager = new DistributorApplicationUserManager(userStore);
                var userToInsert = new DistributorApplicationUser { UserName = userName, Email = email, EmailConfirmed = true, Distributor = distributor };
                IdentityResult result = userManager.Create(userToInsert, password);
            }
            if (!(context.AgentApplicationUsers.Any(u => u.UserName == userName)))
            {
                var userStore = new UserStore<AgentApplicationUser>(context);
                var userManager = new AgentApplicationUserManager(userStore);
                var userToInsert = new AgentApplicationUser { UserName = userName, Email = email, EmailConfirmed = true, Agent = agent };
                IdentityResult result = userManager.Create(userToInsert, password);
            }
            if (!(context.SubagentApplicationUsers.Any(u => u.UserName == userName)))
            {
                var userStore = new UserStore<SubagentApplicationUser>(context);
                var userManager = new SubagentApplicationUserManager(userStore);
                var userToInsert = new SubagentApplicationUser { UserName = userName, Email = email, EmailConfirmed = true, Subagent = subagent };
                IdentityResult result = userManager.Create(userToInsert, password);
            }