简体   繁体   中英

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

Basic structure of Identity looks

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

But I don't need many fields (like EmailConfirmed and some other).

But I need to add some custom fields of simple type ( string, int ) and some field for many-to-many relationship ( List ) same as relationship Users + Roles "Users - UsersRoles - Roles" .

How can this be done without losing functionality and the ability to fully use Identity

You cannot remove any of the built-in properties. They're there to support Identity functionality. Whether or not you're actually requiring email confirmation, it's valuable to know whether the email has been confirmed or not.

Adding additional properties works just like any other entity would. Create a class, if you haven't already, that inherits from IdentityUser , and add whatever properties you like to that.

You can't delete fields on IdentityUser, but you can still add your own - just derive your user class from IdentityUser and then use overload that takes your user class as a type parameter ( services.AddIdentity<MyApplicationUser, IdentityRole>(...) ). Your custom properties will be present whenever you get the user instance from UserManager<MyApplicationUser> . You can then ignore those that you don't want (for example I'd set EmailConfirmed to true when you are creating your users and then forget about it).

Unfortunately that only works for simple data types, if you need custom relations to other entities, your only option might be to replace parts of Identity with your own. I had to do something like this (replace the whole role/claim part of identity with something custom), and it wasn't pretty - back in netcore 2.1 it involved things like writing custom user store and manually removing few services from ServiceCollection after identity registration.

Current netcore seems to have AddIdentityCore<TUser>(this IServiceCollection) and the only requirement on TUser is that it is a reference type ( where TUser: class ), so i'd start there if you really have to. You'll still probably have to implement your own user store that knows how to derive claims from your user class - be prepared to invest at least a day into this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM