-1

MVC5 identity stores userId in a string, but WHY?

If you don't feel like answering THE question you can just post random links how to store user id'z as int's...

tereško
  • 58,060
  • 25
  • 98
  • 150
Greeed
  • 418
  • 2
  • 8
  • 29
  • @marc_s Why does EF identification use stings for id in user table instead of integers? – Greeed Jul 30 '14 at 11:23
  • @Greed, it doesn't just use strings for ids... it can use strings, or integers, or doubles, or any type you want. This is just the default template, customize it to whatever you need. – Erik Funkenbusch Jul 31 '14 at 06:28
  • @Erik Funkenbusch sure kid, I'll tell you the same thing when you'll buy new win 9.0 and the default language will be set to Korean "...you can CHANGE it, yes you can" – Greeed Dec 12 '14 at 09:09

2 Answers2

1

Well, you seem to be quite rude for someone who is quite wrong.

ASP.NET Identity does NOT use strings for Id, it just uses strings for Id by default in the template generated by visual studio for a "starter project". You can use whatever you want for Id by defining a new IdentityUser type. Just like you aren't stuck with using the supplied templates for your views, or controllers, or anything else.

See https://stackoverflow.com/a/23573049/61164

The reason it uses strings is because the default identity type in the starter project template is a Guid, which not all database types support (remember, it supports more than just SQL Server). So, it uses strings to store them for compatibility reasons.

Community
  • 1
  • 1
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

Well I guess it uses a different scheme for Id

If you will check your Database, a sample Id Looks like this

"62e41fbc-dbd6-41d2-8209-0fdf29fb1908"

Assuming this is hash or encrypted value.

If you want to store them as integer or make a Id Count, you can just add another entry to the table (ASPUserId) then make it an int then increment every time you make an entry

  • @ Dude the question is why the F*** the id is stored as a "62e41fbc-dbd6-41d2-8209-0fdf29fb1908" and not as "1" ?? Not how to store it as an int... – Greeed Jul 30 '14 at 14:08
  • Dont need to be rude, as i said the value is in hash or encrypted value. The resoning for this can be many reason but the most imporant is Security, if your Id is in this type of format it will be harder to crack. If you are a hacker and you put your codes to 1,2,3 then its kinda obvious doesn't it? If you want to know more why it is like that, I suggest looking for ASP.Identity Open Source Site. By the way if you really want the int 1, 2, 3 thing then just do what I said and add a new entry in the model. – Franz Justin A. Buenaventura Jul 30 '14 at 14:25
  • If they are in this format then this is a GUID. It is not encryption or hashing. The correct datatype would be `uniqueidentifier`. Not `nvarchar`. – Martin Smith Jul 31 '14 at 05:40
  • @MartinSmith - Not all databases have a guid type, and Identity supports more than just SQL Server. – Erik Funkenbusch Jul 31 '14 at 06:21
  • @ErikFunkenbusch Hmm. OK. But even accepting that argument `nvarchar` would still be a wrong storage choice. The string representation of a GUID only uses ASCII characters so would be more compactly represented (at least in SQL Server standard edition) as `varchar`. Or just a `char` if not nullable as it is fixed length. – Martin Smith Jul 31 '14 at 06:56
  • @MartinSmith - strings in .net are utf-16, so even if the string values are all ascii characters, they're still internally utf-16, and Identity uses reflection to figure out the proper SQL type for a .NET type. It doesn't know that it's going to only be a guid when configuring (you could create your own type that was also string based but did not use a guid, so it can't make assumptions). – Erik Funkenbusch Jul 31 '14 at 07:06