Why asp.net Identity user id is string?

后端 未结 2 571
悲哀的现实
悲哀的现实 2020-12-28 16:21

I want to use System.Guid type as an id for all of my tables in asp.net web api application. But I also use Asp.net Identity, which using a string-type id

2条回答
  •  一生所求
    2020-12-28 16:51

    With ASP.NET Core, you have a very simple way to specify the data type you want for Identity's models.

    First step, override identity classes from < string> to < data type you want> :

    public class ApplicationUser : IdentityUser
    {
    }
    
    public class ApplicationRole : IdentityRole
    {
    }
    

    Declare your database context, using your classes and the data type you want :

    public class ApplicationDbContext : IdentityDbContext
        {
            public ApplicationDbContext(DbContextOptions options)
                : base(options)
            {
            }
    
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(builder);
                // Customize the ASP.NET Identity model and override the defaults if needed.
                // For example, you can rename the ASP.NET Identity table names and more.
                // Add your customizations after calling base.OnModelCreating(builder);
            }
        }
    

    And in your startup class, declare the identity service using your models and declare the data type you want for the primary keys :

    services.AddIdentity()
                .AddEntityFrameworkStores()
                .AddDefaultTokenProviders();
    

提交回复
热议问题