I am running into a problem with the default code for adding an external login via ASP.NET Identity.
I have the following code in my Account Controller ExternalLoginConfirmation method:
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
//do stuff
}
}
The CreateAsync call returns result.Succeeded==true. However, when it flows through the AddLoginAsync method, that method returns result.Succeeded==false with the following errors:
Name foo@bar.com is already taken.
Email 'foo@bar.com' is already taken.
When I look in the aspnetusers table, I see the new row for this user as expected from the CreateUserAsync method. If AddLoginAsync is supposed to add a login to an existing user, why is it throwing the error like it was expecting the user to not exist? You cannot call CreateAsync without previously populating UserName and Email in your new user instance.
The only difference between the out-of-the-box Identity setup is I have created a customer ApplicationUser class that uses an int for the Id instead of string.
Any ideas?