Patrick Desjardins Blog
Patrick Desjardins picture from a conference

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

Posted on: 2014-05-15

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.