简体   繁体   中英

Identity on ASP.NET Core AspNetUsers

I just start to working with identity and i´m having a conceptual doubt about AspNetUsers primary key. The default PK is a nvarchar(450), this is supposed to identify this account on the system. So my doubt is in a real world application this PK is used as FK in other system tables to identitfy the account? 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? Another thing, about the search performance of having the nvarchar as FK in other table?

The base class you get to start out with is IdentityUser , but you are correct in that is uses a string as a PK. This default maps to the email address provided at registration, which further complicates things. Not ideal for use as a FK anywhere.

I prefer Int values for PK's, but you can set it to others as well and GUIDs are common.

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. Just make sure you get ApplicationUser updated everywhere you need it or things can get rather gnarly.

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.

You can always extend the base Identity 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; }
}

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