【问题标题】:ASP.Net Identity Role restricted database entriesASP.Net 身份角色限制数据库条目
【发布时间】:2019-02-26 23:07:49
【问题描述】:

我一直在对我的项目进行更改以合并 ASP.Net Identity。 现在我已经停止了一个特定的问题。 我有一个名为 Files 的模型,我必须能够添加只有特定角色的用户才能看到的文件。 假设我添加了一份仅供开发人员、测试人员和管理员使用的报告,但 HumanResources 和 Accounting 不应该看到该条目。

我这样指定我的模型:

public class FileModel
{
    [Key]
    [Required]
    public Guid Id { get; set; }

    [Required]
    [Display(Name = "File Name")]
    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(280)]
    public string Description { get; set; }

    [Required]
    public string Filetype { get; set; }

    [Required]
    public string Path { get; set; }

    [Required]
    [Display(Name = "Allowed Users")]
    public virtual List<IdentityRole> Roles { get; set; }
}

我的期望是我能够将角色添加到文件中,但即使我在模型中指定了一个 IdentityRole 列表,当我迁移它时,它从未在数据库中创建来自 FileModel 和角色的映射,并且有在 FileModel 表中没有提到角色。有没有更好的方法来解决这个问题?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-identity


    【解决方案1】:

    如果您想添加角色列,然后您可以将您的用户与文件模型进行比较

        ......
        [Required]
        public string RoleId{ get; set; }
        [ForeignKey("RoleId")]
        public IdentityRole Roles{ get; set; } 
    

    或者您可以使用两个 ForeignKeys - FilemodelId 和 RoleId 创建一个中间表

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2011-06-22
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多