[英]AspNetCore.Identity not working with custom User/Role implementation
[英]How to delete basic fields and add custom fields on AspNetCore.Identity?
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 + Roles "Users - UsersRoles - Roles"相同。
如何在不丟失功能和充分使用身份的能力的情況下做到這一點
您不能刪除任何內置屬性。 他們在那里支持身份功能。 無論您是否真的需要 email 確認,了解 email 是否已確認是很有價值的。
添加其他屬性的工作方式與任何其他實體一樣。 創建一個繼承自IdentityUser
的 class(如果您還沒有的話),然后添加您喜歡的任何屬性。
您不能刪除 IdentityUser 上的字段,但您仍然可以添加自己的字段 - 只需從 IdentityUser 派生您的用戶 class ,然后使用將您的用戶 class 作為類型參數的重載( services.AddIdentity<MyApplicationUser, IdentityRole>(...)
)。 每當您從UserManager<MyApplicationUser>
獲取用戶實例時,您的自定義屬性就會出現。 然后,您可以忽略不想要的那些(例如,當您創建用戶時,我將 EmailConfirmed 設置為 true,然后忘記它)。
不幸的是,這只適用於簡單的數據類型,如果您需要與其他實體的自定義關系,您唯一的選擇可能是用您自己的部分替換 Identity。 我不得不做這樣的事情(用自定義的東西替換整個角色/聲明部分身份),這並不漂亮 - 在 netcore 2.1 中,它涉及編寫自定義用戶存儲和在身份后手動從 ServiceCollection 中刪除一些服務登記。
當前的 netcore 似乎有AddIdentityCore<TUser>(this IServiceCollection)
並且對 TUser 的唯一要求是它是一個引用類型( where TUser: class
),所以如果你真的需要,我會從那里開始。 您可能仍然需要實現自己的用戶商店,該商店知道如何從您的用戶 class 獲得聲明 - 准備至少投入一天的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.