Home » ASP » ASP.MVC » CreateIdentityAsync value cannot be null when logging with User created with Migration Tool

CreateIdentityAsync value cannot be null when logging with User created with Migration Tool

If you create a user from your Asp.Net MVC (Identity) and logging with this one, it should work. However, if you create your users by code, for example with Entity Framework Migration seeding method, you may have for result an error page. The error is not very clear. It can say that the value cannot be null and that the parameter name is value. This does not give a lot of information about where the error is. The stack trace shows some information about security claim and the problem is trigged by the call to the UserManager CreateIdentityAsync method.


In fact, the problem is that the Security Stamp was not set. This was found after seeing a difference in AspNetUsers table.


As you can see, the column was not set for user created by Entity Framework Migration Seed method.

To solve this issue, the seed method changed to generate a Security Stamp. The security stamp can be generated with a random GUID.

var adminUser = new ApplicationUser { Id = Guid.NewGuid().ToString()
	, FirstName = "Patrick"
	, LastName = "Desjardins"
	, DisplayName = "pdesjardins"
	, Email = EMAIL_ADMIN
	, CreationDateTime = runningContext.GetCurrentTime()
	, UserName = userValueGenerator.GenerateUserName(EMAIL_ADMIN), PasswordHash = ApplicationUser.HashPassword("123123")
	, ValidationDateTime = runningContext.GetCurrentTime()
	, SecurityStamp = Guid.NewGuid().ToString()

With the Security Stamp set, it is possible to login without having CreateIdentityAsync raising any exception.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

3 Responses so far.

  1. novacp says:

    thanks for this Post.
    One thing is missing for me to understand the whole problem. What is the “seed method” and where do I find it / have to implement it?

    Best regards.

    • Entity Framework lets you override a method to insert data into the database when this one is generated. The goal is to have initial values into the database. The Seed method is from System.Data.Entity.Migrations.DbMigrationsConfiguration class and is available only if you are using Migration Tool.

  2. Daniel says:

    Thanks for this post. I had totally forgot that I had added the Security Stamp column after the users were created. Newly created users were fine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.