1
votes

I've created a custom UserStore class in an ASP.NET MVC 5 web site to allow custom reading/writing to SQL Server, and I have a couple of questions...

  1. When 'var result = await UserManager.CreateAsync(user, model.Password);' is called from the controller 'public Task FindByNameAsync(string userName)' in UserStore is called first and then 'public Task CreateAsync(TUser user)' is called. What stops a second account of the same username from being created at the same time?

  2. How can I raise errors in 'public Task CreateAsync(TUser user)' that results in 'result.Succeeded == false' and 'result errors' in the controller.

1

1 Answers

1
votes

For #1, we use ValidateEntity on IdentityDbContext to ensure that user names are unique. And in the 2.0 release we are adding a unique index on user names as well which should guarantee they are unique.

For #2, Stores are expected to throw exceptions when operations fail, the basic CRUD operations are not expected to really fail normally. If you have special behavior that your store wants to expose, you either override or implement your own variant of CreateAsync that returns the appropriate IdentityResult with the error string you desire.