简体   繁体   English

ASP.NET 核心 AspNetUsers 上的身份

[英]Identity on ASP.NET Core AspNetUsers

I just start to working with identity and i´m having a conceptual doubt about AspNetUsers primary key.我刚开始使用身份,我对 AspNetUsers 主键有概念上的疑问。 The default PK is a nvarchar(450), this is supposed to identify this account on the system.默认 PK 是 nvarchar(450),这应该在系统上识别此帐户。 So my doubt is in a real world application this PK is used as FK in other system tables to identitfy the account?所以我怀疑在现实世界的应用程序中,这个 PK 在其他系统表中用作 FK 来识别帐户? Or the normal thing to do is to have somekind of intermediate user that has a PK as int and the FK is the nvarchar value?或者正常的做法是让某种中间用户具有 PK 作为 int 并且 FK 是 nvarchar 值? Another thing, about the search performance of having the nvarchar as FK in other table?另一件事,关于在其他表中将 nvarchar 作为 FK 的搜索性能?

The base class you get to start out with is IdentityUser , but you are correct in that is uses a string as a PK.您开始使用的基础 class 是IdentityUser ,但您是正确的,因为它使用字符串作为 PK。 This default maps to the email address provided at registration, which further complicates things.此默认映射到注册时提供的 email 地址,这使事情变得更加复杂。 Not ideal for use as a FK anywhere.不适合在任何地方用作 FK。

I prefer Int values for PK's, but you can set it to others as well and GUIDs are common.我更喜欢 PK 的 Int 值,但您也可以将其设置为其他值,并且 GUID 很常见。

See this thread for further details. 有关更多详细信息,请参阅此线程。 , and you can replace the GUID in their description with int , and let EF Core manage the migration to get you where you need to be. ,您可以将其描述中的GUID替换为int ,并让 EF Core 管理迁移以将您带到您需要的位置。 Just make sure you get ApplicationUser updated everywhere you need it or things can get rather gnarly.只要确保您在需要的任何地方都更新 ApplicationUser,否则事情会变得相当粗糙。

Then if you have anything that needs to associate a post with a user (as an example), you can do it by a numerical PK and keep the personal info (email) private, and in case the user ever deletes it you still have a number attached.然后,如果您有任何需要将帖子与用户相关联(例如),您可以通过数字 PK 来完成,并将个人信息(电子邮件)保密,如果用户删除它,您仍然有附号码。

You can always extend the base Identity class.您始终可以扩展基本身份 class。 I've done this:我已经这样做了:

public interface IUser : IAssetsBase
{
    int SiteId { get; set; }
    Site Site { get; set; }
    string Token { get; set; }
}

public class User : IdentityUser<int>, IUser
{
    [PersonalData]
    public bool Active { get; set; } = true;
    [PersonalData]
    public DateTime DateCreated { get; set; } = DateTime.Now;
    [PersonalData]
    public DateTime? DateDeleted { get; set; }
    [PersonalData]
    public Guid UId { get; set; } = Guid.NewGuid();
    [PersonalData]
    public int SiteId { get; set; }
    [PersonalData]
    public virtual Site Site { get; set; }
    [PersonalData]
    public string Token { get; set; }
}

public interface IAssetsBase
{
    int Id { get; set; }
    bool Active { get; set; }
    Guid UId { get; set; }
    DateTime DateCreated { get; set; }
    DateTime? DateDeleted { get; set; }
}

public abstract class AssetsBase : IAssetsBase
{
    public int Id { get; set; }
    public bool Active { get; set; } = true;
    public Guid UId { get; set; } = Guid.NewGuid();
    public DateTime DateCreated { get; set; } = DateTime.Now;
    public DateTime? DateDeleted { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否将用户设置存储在ASP.NET Core身份AspNetUsers表中 - Store User Settings in ASP.NET Core Identity AspNetUsers Table or Not Asp.net核心 - 没有这样的表:AspNetUsers - Asp.net core - no such table: AspNetUsers dbo.AspNetUsers 表上的 ASP.NET MVC 5 核心标识 CompanyID 外键 - ASP.NET MVC 5 Core Identity CompanyID foreign key on dbo.AspNetUsers table 如何有条件地将 map 多个模型添加到 ASP.NET 核心标识中的一张表(AspNetUsers)? - How to conditionally map multiple models to one table (AspNetUsers) in ASP.NET Core Identity? ASP.NET Identity 从 AspNetUsers 表中删除列 - ASP.NET Identity remove column from AspNetUsers table ASP.NET MVC-将Identity与现有的经过修改的AspNetUsers表一起使用 - ASP.NET MVC - Using Identity with existing, modified AspNetUsers table 我想为 ASP.NET 核心标识中的用户创建一个帐户,但我无法将数据插入表 dbo.AspNetUsers - I want to create an account for a user in ASP.NET Core identity but I can't insert the data into table dbo.AspNetUsers Asp.Net Core Identity 核心注册 - Asp.Net Core Identity Core Registration 使用ASP.Net Identity EntityFramwork到ID列设置为UniqueIdentifier的现有AspNetUsers表 - Using ASP.Net Identity EntityFramwork to Existing AspNetUsers table with ID Column set to UniqueIdentifier ASP.NET标识 - 如何将dbo.AspNetUsers.Id更改为非聚簇索引? - ASP.NET Identity - How to change the dbo.AspNetUsers.Id into a nonclustered index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM