AspNetCore.Identity 如何删除基本字段和添加自定义字段?

How to delete basic fields and add custom fields on AspNetCore.Identity?

基本结构身份长相

using Microsoft.AspNetCore.Identity;
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.

        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();
    }
}

但我不需要很多字段(比如 EmailConfirmed 和其他一些字段)。

但我需要添加一些简单类型的自定义字段(string,int)和一些多对多关系的字段(List) 与关系用户 + 角色相同 "Users - UsersRoles - Roles".

如何在不丢失功能和充分利用 Identity

的情况下做到这一点

您不能删除任何内置属性。他们在那里支持身份功能。无论您是否真的需要电子邮件确认,了解电子邮件是否已被确认都很有价值。

添加额外的属性就像任何其他实体一样。创建一个 class,如果您还没有,它继承自 IdentityUser,并向其添加您喜欢的任何属性。

您不能删除 IdentityUser 上的字段,但您仍然可以添加自己的字段 - 只需从 IdentityUser 派生您的用户 class,然后使用将您的用户 class 作为类型参数的重载(services.AddIdentity<MyApplicationUser, IdentityRole>(...))。只要您从 UserManager<MyApplicationUser> 获取用户实例,您的自定义属性就会出现。然后您可以忽略那些您不想要的(例如,我会在您创建用户时将 EmailConfirmed 设置为 true 然后忘记它)。

不幸的是,这只适用于简单的数据类型,如果您需要与其他实体的自定义关系,您唯一的选择可能是用您自己的部分替换身份。我不得不做这样的事情(用自定义的东西替换身份的整个 role/claim 部分),它并不漂亮 - 在 netcore 2.1 中它涉及编写自定义用户存储和手动从 ServiceCollection 中删除一些服务身份注册后

当前的 netcore 似乎有 AddIdentityCore<TUser>(this IServiceCollection) 并且对 TUser 的唯一要求是它是一个引用类型 (where TUser : class),所以如果你真的需要的话我会从那里开始。您可能仍然需要实现自己的用户存储,该存储知道如何从您的用户那里获取声明 class - 准备为此至少投入一天时间。