29

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 (to store guids as well). So I wonder why is it using string id instead of System.Guid by default? And what is better choice to use through all the application - Guid id or string-guid id? In case of using string - what is the most proper and reliable way to generate new id - in code or in database?

Ostap
  • 353
  • 1
  • 4
  • 8
  • 1
    The "why" is explained [here](http://stackoverflow.com/q/19238621/304683) - both answers in that post will really help. Hth. – EdSF May 04 '15 at 14:09
  • Thanks @EdSF, but I still have an open question on what is the best way to organize application - string-guids or Guids (uniqueidentifier) for id? – Ostap May 04 '15 at 15:09
  • 1
    It depends on _your_ requirements. See the [answer of Rick Anderson (MSFT)](http://stackoverflow.com/a/24152085/304683) for guidance. – EdSF May 04 '15 at 18:18

2 Answers2

14

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<Guid>
{
}

public class ApplicationRole : IdentityRole<Guid>
{
}

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

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> 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<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
            .AddDefaultTokenProviders();
AdrienTorris
  • 9,111
  • 9
  • 34
  • 52
  • 3
    For information, there is a topic on that in the official documentation now : https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-primary-key-configuration – AdrienTorris Jan 11 '17 at 09:09
3

Depending on which version of ASP.Net authentication you're using, on the database ASP.NET Identity v2 should be storing it as a uniqueidentifier (Guid) in the AspNetUsers table. In more preceding versions it will store the user id as an int in the webpages_Membership table.

I believe it surfaces it as a string so it can be any data type you like under the hood and this can then be cast within your code as required.

Deilan
  • 4,740
  • 3
  • 39
  • 52
Tim
  • 5,443
  • 2
  • 22
  • 26
  • Hi Tim, I am using the latest version of membership system for ASP.NET - ASP.NET Identity. I am still not sure, what is the best practice to use string for all the tables in my database or convert everything to GUID? – Ostap May 04 '15 at 15:00
  • @user3460585 int vs Guids are age-old argument. See these answers: http://dba.stackexchange.com/a/266/6868 http://blog.codinghorror.com/primary-keys-ids-versus-guids/ http://programmers.stackexchange.com/a/189031/86760 – trailmax May 05 '15 at 20:03
  • 1
    Sorry, I didn't realise you were asking for your database. Generally what we do is store the username against a User record in our database which we can then reference the ASP.Net record, it also detaches you from the underlying membership tier. – Tim May 06 '15 at 17:27