Configure Identity primary keys data type

ASP.NET Core Identity allows you to easily configure the data type you want for the primary keys. By default, Identity uses string data type but you can very quickly override this behavior.

How to

  1. The first step is to implement the Identity's model, and override the string type with the data type you want.

    namespace webapptemplate.Models
        // Add profile data for application users by adding properties to the ApplicationUser class
        public class ApplicationUser : IdentityUser<Guid>
    namespace webapptemplate.Models
        public class ApplicationRole : IdentityRole<Guid>
  2. Implement the database context of Identity with your models and the data type you want for primary keys

    namespace webapptemplate.Data
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            protected override void OnModelCreating(ModelBuilder 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);
  3. Use your models and the data type you want for primary keys when you declare the identity service in your application's startup class

    public void ConfigureServices(IServiceCollection services)
        // Add framework services.
            .AddDbContext<ApplicationDbContext>(options =>
        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        // Configure Identity
        services.Configure<IdentityOptions>(options =>
            // Password settings
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 8;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = true;
            options.Password.RequireLowercase = false;
            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
            options.Lockout.MaxFailedAccessAttempts = 10;
            options.Lockout.AllowedForNewUsers = true;
            // Cookie settings
            options.Cookies.ApplicationCookie.CookieName = "YouAppCookieName";
            options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(150);
            options.Cookies.ApplicationCookie.LoginPath = "/Account/LogIn";
            options.Cookies.ApplicationCookie.LogoutPath = "/Account/LogOff";
            options.Cookies.ApplicationCookie.AccessDeniedPath = "/Account/AccessDenied";
            options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
            options.Cookies.ApplicationCookie.AuthenticationScheme = Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme;