Asp.Net Core中扩展IdentityUser类

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

#region 程序集 Microsoft.Extensions.Identity.Stores, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// C:\Users\ysadmin\.nuget\packages\microsoft.extensions.identity.stores\5.0.0\lib\netstandard2.0\Microsoft.Extensions.Identity.Stores.dll
#endregion

using System;

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.
        //
        // 值:
        //     True if 2fa is enabled, otherwise false.
        [PersonalData]
        public virtual bool TwoFactorEnabled { get; set; }
        //
        // 摘要:
        //     Gets or sets a flag indicating if a user has confirmed their telephone address.
        //
        // 值:
        //     True if the telephone number has been confirmed, otherwise false.
        [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.
        //
        // 值:
        //     True if the email address has been confirmed, otherwise false.
        [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.
        //
        // 值:
        //     True if the user could be locked out, otherwise false.
        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();
    }
}

定义ApplicationUser类,并继承IdengtityUser类

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyPractice.Models
{
    public class ApplicationUser:IdentityUser
    {
        public string City { get; set; }
    }
}

查找所有引用IdentityUser的类,并替换为ApplicationUser。
在这里插入图片描述
 然后在AppDbContext中继承泛型 IdentityDbContext类。

    public class AppDbContext : IdentityDbContext<ApplicationUser>

重新做数据迁移
实体类使用新增字段

[Display(Name = "城市")]
        public string City { get; set; }

视图

<div class="form-group">
                <label asp-for="City"></label>
                <input asp-for="City" class="form-control" />
            </div>

效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值