【问题标题】:Different profiles for different roles in Asp.net Identity.Asp.net Identity 中不同角色的不同配置文件。
【发布时间】:2015-06-13 01:24:56
【问题描述】:

是否可以为不同的角色提供不同的个人资料?还是我必须:

  1. 创建包含所有共享字段的配置文件,即名字、姓氏。
  2. 创建两个单独的表,每个表都包含自己独特的属性,即表 1:DOB 表 2:地址和指向配置文件的链接?

【问题讨论】:

    标签: c# asp.net roles asp.net-identity-2 profiles


    【解决方案1】:

    您可以像这样使用您的身份用户,然后添加新的实体教师和学生:

    public class User: IdentityUser
    {
        [Required]
        [StringLength(100)]
        public string FirstName { get; set; }
    
        [Required]
        [StringLength(100)]
        public string LastName { get; set; }
    
        public DateTime JoinedOn { get; set; }
        public virtual Student Student { get; set; }
    
        public virtual Teacher Teacher { get; set; }
    
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(
            UserManager<User, Guid> 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;
        } 
    }
    

    【讨论】:

      【解决方案2】:

      您可以为不同的角色创建不同的个人资料。假设您有一个 User 表,并且您有一些角色,例如学生、教师、管理员。您可以在用户和学生、用户和教师、用户和管理员之间创建一对一的关系。特定于 Student 的属性保留在 Student 表中。

      编辑1:

      所以你是对的,你可以创建具有公共属性的用户和具有特定于它们的属性的其他表。

      【讨论】:

      • 我能否通过 Identity API 访问学生、教师、管理员数据,还是必须自己将其映射到我的模型?
      猜你喜欢
      • 2014-05-05
      • 2016-06-27
      • 2019-04-11
      • 2012-01-03
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多