首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET MVC-5中的多个登录实体和多级访问权限

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

Stack Overflow用户
提问于 2015-11-11 03:33:56
回答 2查看 1.8K关注 0票数 0

我正在建立一个系统,其中有4层登录,有4个实体涉及。

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

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

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

如有任何帮助,将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2015-11-11 06:19:51

您不需要为每个用户创建四个单独的项目。您可以在一个项目中完成这个任务。

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

我可以解释一下做这件事的基本想法。

步骤1

为每个用户创建四个角色

步骤2

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

步骤3

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

步骤4

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

步骤5

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

步骤6

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

就这样!

票数 1
EN

Stack Overflow用户

发布于 2015-11-20 05:03:01

我创建了5个项目

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

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

我使用的是Microsoft.AspNet.Identity版本2,IdentityDbContext.ValidateEntity方法上有一个bug,它将检查表AspNetUsers中UserName的唯一规则。(因为当不同的AspNetUsers从IdentityUser继承的用户名或电子邮件相同时,针对IdentityUser表的检查会导致错误)

最后,我直接从DbContext类继承,并将标识类放在那里。还删除了ValidateEntity方法

以下是我的上下文类代码:

代码语言:javascript
复制
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; }
}

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

代码语言:javascript
复制
            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);
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33643841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档