ASP.Net 5 Identity Framework 3.0 manually adding users

Identity Framework 3 is in release candidate along with ASP.Net 5.

The problem

I set up a web project in ASP.Net 5 and created a controller for user import. It’s just some simple code that attempts to add multiple users. Adding a single user works great, but if I attempt to perform multiple operations in one go I run into problems.

  • Adding multiple users gives me “Collection was modified; enumeration operation may not execute” on second user.
  • Attempting to update the password for more multiple users gives me “Cannot access a disposed object” on second user.

The problem turned out to be me calling my commands in “async void” methods. (public async void MyMethod(…) { … }). Changing it to “public async Task” instead and awaiting it to finish seems to solve the problem.

But in case someone needs it, here is the solution I used for manually adding users.

My solution

By digging into the source code of Identity Framework I figured out how to fill the user objects myself.

Here is a simple method to get you going. Note that it required your DbContext to inherit from IdentityDbContext<ApplicationUser>.


 

2 thoughts on “ASP.Net 5 Identity Framework 3.0 manually adding users”

Leave a Reply

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

%d bloggers like this: