【问题标题】:How do I add fields to the AspNetUsers table in Asp.Net Identity Framework?如何向 Asp.Net Identity Framework 中的 AspNetUsers 表添加字段?
【发布时间】:2014-06-28 07:26:51
【问题描述】:

我正在探索新的 asp.net 身份。使用代码优先和迁移功能向用户表“AspNetUsers”添加字段似乎很棒。

我想添加“Name”、“CreatedOn”和“CreatedFromIP”等列,并能够从 .NET 中读取它

有简单的解决方案吗?

【问题讨论】:

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


    【解决方案1】:

    您可以简单地从 App_Start 文件夹中打开数据库并修改字段。 (如果您使用模板)

    【讨论】:

    • 你确定吗?该表是使用 EF Code First 创建的,并根据迁移索引检查其架构。那么如何制止冲突呢?\
    【解决方案2】:
    public class ApplicationUser : IdentityUser
    {
        public string YourProperyHere { 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;
        }
    }
    

    阅读更多here

    【讨论】:

      【解决方案3】:

      Visitor 类在您设置新的身份框架项目时作为模板提供给您,并且该类与AspNetUsers 表相关联。它继承了一堆您无法控制/无法控制的属性,然后您可以添加任何您想要的东西。

      public class Visitor : IdentityUser
      {
          [MaxLength(300)]
          public string FirstName { get; set; }
      
          [MaxLength(300)]
          public string LastName { get; set; }
      
          public DateTime CreatedOn { get; set; }
      
      
          public Visitor()
          {
              CreatedOn = DateTime.UtcNow;
          }
      }
      

      我还没有找到修改甚至直接查询 AspNetUserLogins 表的方法,因为默认的身份框架设置为您提供了一个可以访问用户和角色的数据库,但不能访问登录名。此外,该表是从用户那里降级的,但是,这不会让您查询它。而且,至少默认情况下,您会坚持使用 Identity Framework 附带的 stock 类;将其子类化并让框架使用该子类可能会被排除在外。不知道,值得自己提问:

      Add Columns/Properties to AspNetUserLogins/Logins in IdentityDbContext

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-22
        • 2020-04-20
        • 2014-12-04
        • 2015-10-27
        • 2021-07-15
        • 2017-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多