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

问题描述 投票:0回答:1

基本结构 身份认证 样子

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和其他一些)。

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

如何才能在不失去功能和能力的前提下,充分使用 身份认证

c# asp.net-mvc asp.net-core asp.net-identity code-first
1个回答
2
投票

你不能删除任何内置属性。它们是为了支持Identity功能而存在的。无论你是否真的需要电子邮件确认,知道电子邮件是否已经被确认是很有价值的。

添加额外的属性就像其他实体一样。如果您还没有创建一个类,那么就创建一个继承于 IdentityUser,然后添加任何你喜欢的属性到其中。


0
投票

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

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

当前的netcore似乎有 AddIdentityCore<TUser>(this IServiceCollection) 而对TUser的唯一要求是它是一个引用类型(where TUser : class),所以如果你真的有必要的话,我会从那里开始。你可能还需要实现你自己的用户商店,它知道如何从你的用户类中派生出声明--你要准备好至少投入一天的时间在这上面。

© www.soinside.com 2019 - 2024. All rights reserved.