Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?

What is the purpose of the ConcurrencyStamp column in the AspNetUsers table in the new ASP.NET MVC 6 identity?

This is the database schema of the AspNetUsers table:

enter image description here

It is also there in the AspNetRoles table:

enter image description here

As I remember it wasn't there in the ASP.NET MVC 5 identity.

What I've noticed so far is that it seems to have GUID values as it is defined with the following code:

/// <summary> /// A random value that must change whenever a user is persisted to the store /// </summary> public virtual string ConcurrencyStamp { get; set; } = Guid.NewGuid().ToString(); 

But this documentation is not sufficient for me to understand in which situations it is used.

like image 772
Nikolay Kostov Avatar asked Dec 13 '15 15:12

Nikolay Kostov


People also ask

What is ConcurrencyStamp in asp net core?

The summary of both properties in the entity IdentityUser are in English: ConcurrencyStamp: A random value that should change whenever the user is persisted to the store. Translating would be something more or less like this: A random value that should change whenever a user persists in the repository.

What is concurrency stamp identity?

A stamp that is used to identify the current version of the data. If you change it, so does the stamp. So if two concurrent updates comes in at the same time, they must have the same stamp or one of them should be discarded. Hence the name, ConcurrencyStamp .

What is identity in asp net core?

ASP.NET Core Identity provides a framework for managing and storing user accounts in ASP.NET Core apps. Identity is added to your project when Individual User Accounts is selected as the authentication mechanism. By default, Identity makes use of an Entity Framework (EF) Core data model.

What is AspNetUserClaims table?

AspNetUserClaims” table is holding claims assigned to a user. A claim is different from a role because a claim is a key-value pair. You can have a role or not have a role. Claim also provides a value for a specified claim. In a way, it is like an optional property assigned to a user.


1 Answers

As the name state, it's used to prevent concurrency update conflict.

For example, there's a UserA named Peter in the database 2 admins open the editor page of UserA, want to update this user.

  1. Admin_1 opened the page, and saw user called Peter.
  2. Admin_2 opened the page, and saw user called Peter (obviously).
  3. Admin_1 updated user name to Tom, and save data. Now UserA in the db named Tom.
  4. Admin_2 updated user name to Thomas, and try to save it.

What would happen if there's no ConcurrencyStamp is Admin_1's update will be overwritten by Admin_2's update. But since we have ConcurrencyStamp, when Admin_1/Admin_2 loads the page, the stamp is loaded. When updating data this stamp will be changed too. So now step 5 would be system throw exception telling Admin_2 that this user has already been updated, since he ConcurrencyStamp is different from the one he loaded.

like image 87
Steven.Xi Avatar answered Sep 23 '22 14:09

Steven.Xi