Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why asp.net Identity user id is string?

Tags:

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?

like image 561
Ostap Avatar asked May 04 '15 11:05

Ostap


People also ask

What is identity user in asp net?

ASP.NET Identity is the membership system for authentication and authorization of the users by building an ASP.NET application. ASP.NET Identity is the membership system for authentication and authorization of the users by building an ASP.NET application.

How does ASP NET identity work?

ASP.NET Core Identity is a membership system which allows you to add login functionality to your application. Users can create an account and login with a user name and password or they can use an external login providers such as Facebook, Google, Microsoft Account, Twitter and more.

What is user identity in C#?

Identity is Users Authentication and Authorization. In this article we will see how users are able to log in with their social identities so that they can have a rich experience on their website. Description. Identity is Users Authentication and Authorization.


1 Answers

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(); 
like image 122
AdrienTorris Avatar answered Oct 08 '22 16:10

AdrienTorris