• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

扩展Asp.NetCore中的IdentityUser类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  虽然Asp.Net Core.Identity提供了IdentityUser类,但是在有些情况下我们需要一些额外的用户信息,比如性别,年龄等,这时候就需要来扩展IdentityUser类以达到我们的需求。

namespace Microsoft.AspNetCore.Identity
{
    //
    // 摘要:
    //     Represents a user in the identity system
    //
    // 类型参数:
    //   TKey:
    //     The type used for the primary key for the user.
    public class IdentityUser<TKey> where TKey : IEquatable<TKey>
    {
        //
        // 摘要:
        //     Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.
        public IdentityUser();
        //
        // 摘要:
        //     Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.
        //
        // 参数:
        //   userName:
        //     The user name.
        public IdentityUser(string userName);

        //
        // 摘要:
        //     Gets or sets the date and time, in UTC, when any user lockout ends.
        //
        // 言论:
        //     A value in the past means the user is not locked out.
        public virtual DateTimeOffset? LockoutEnd { get; set; }
        //
        // 摘要:
        //     Gets or sets a flag indicating if two factor authentication is enabled for this
        //     user.
        [PersonalData]
        public virtual bool TwoFactorEnabled { get; set; }
        //
        // 摘要:
        //     Gets or sets a flag indicating if a user has confirmed their telephone address.
        [PersonalData]
        public virtual bool PhoneNumberConfirmed { get; set; }
        //
        // 摘要:
        //     Gets or sets a telephone number for the user.
        [ProtectedPersonalData]
        public virtual string PhoneNumber { get; set; }
        //
        // 摘要:
        //     A random value that must change whenever a user is persisted to the store
        public virtual string ConcurrencyStamp { get; set; }
        //
        // 摘要:
        //     A random value that must change whenever a users credentials change (password
        //     changed, login removed)
        public virtual string SecurityStamp { get; set; }
        //
        // 摘要:
        //     Gets or sets a salted and hashed representation of the password for this user.
        public virtual string PasswordHash { get; set; }
        //
        // 摘要:
        //     Gets or sets a flag indicating if a user has confirmed their email address.
        [PersonalData]
        public virtual bool EmailConfirmed { get; set; }
        //
        // 摘要:
        //     Gets or sets the normalized email address for this user.
        public virtual string NormalizedEmail { get; set; }
        //
        // 摘要:
        //     Gets or sets the email address for this user.
        [ProtectedPersonalData]
        public virtual string Email { get; set; }
        //
        // 摘要:
        //     Gets or sets the normalized user name for this user.
        public virtual string NormalizedUserName { get; set; }
        //
        // 摘要:
        //     Gets or sets the user name for this user.
        [ProtectedPersonalData]
        public virtual string UserName { get; set; }
        //
        // 摘要:
        //     Gets or sets the primary key for this user.
        [PersonalData]
        public virtual TKey Id { get; set; }
        //
        // 摘要:
        //     Gets or sets a flag indicating if the user could be locked out.
        public virtual bool LockoutEnabled { get; set; }
        //
        // 摘要:
        //     Gets or sets the number of failed login attempts for the current user.
        public virtual int AccessFailedCount { get; set; }

        //
        // 摘要:
        //     Returns the username for this user.
        public override string ToString();
    }
}

  接下来我们就来定义一个继承自IdentityUser类的ApplicationUser类。

public class ApplicationUser : IdentityUser
{
    public string City { get; set; }
}

  然后在AppDbContext中继承泛型 IdentityDbContext<ApplicationUser>类。并且在Startup中注入的时候把IdentityUser修改为ApplicationUser后,再做数据库迁移,如果之前已经迁移过,则需要替换项目中所有的IdentityUser为ApplicationUser。

public class AppDbContext:IdentityDbContext<ApplicationUser>
{

    public AppDbContext(DbContextOptions<AppDbContext> options):base(options)
    {
    }

    public DbSet<Student> Students { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Seed();
    }
}
services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddErrorDescriber<CustomIdentityErrorDescriber>()
    .AddEntityFrameworkStores<AppDbContext>();

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETDatabaseCacheDependency发布时间:2022-07-10
下一篇:
asp.net页面局部刷新发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap