Search code examples
asp.netentity-frameworkasp.net-web-apiasp.net-identity

How to update security stamp of users in bulk? Asp.net Identity


I am updating users security stamp when a user is being deactivated. I can do this for one user like this.

await UserManager.UpdateSecurityStampAsync(userId);

I want to do this form multiple users, like 500 or may bee 100. Is there any way I can do this in bulk?

I am using entity framework.


Solution

  • Security Stamp is a random string with no significant importance of the actual value, so you can just stuff new GUID there.

    Best way to do it in bulk would be to execute SQL query:

    update ApplicationUsers set SecurityStamp = NEWID()
    

    You can specify where clause as needed to limit users you need to update, and most likely you'll need to change the name of the table to what you actually have in your DB.

    There different ways to execute this query - you can do this with EF:

    context.Database.ExecuteSqlCommand("UPDATE ApplicationUsers........"); 
    

    Or you can look into already recommended EF.Extended library - bulk update is possible without dipping into SQL.