UserManager.FindByEmailAsync
returns null
, but the user exists in the database.
The code below explain the strange problem:
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
var test = new Data.ApplicationDbContext().Users.First(x => x.NormalizedEmail == email);
var usermail = await _userManager.FindByEmailAsync(email);
Console.WriteLine(test == null); //false
Console.WriteLine(usermail == null); //true
EDIT
Also via _userManager
itself, the desired user is obtained:
var test = _userManager.Users.FirstOrDefault(x => x.NormalizedEmail == email);
var usermail = await _userManager.FindByEmailAsync(email);
Console.WriteLine(test == null); //false
Console.WriteLine(usermail == null); //true
It should be noted that the user was not created in a "conventional" manner, but by Data-Seed (in OnModelCreating
):
protected override void OnModelCreating(ModelBuilder builder)
{
var users = new (string email, string name)[] {
("[email protected]", "admin")
};
var appUsers = users.Select(x => new ApplicationUser
{
Email = x.email,
NormalizedEmail = x.email,
NormalizedUserName = x.email,
UserName = x.email,
EmailConfirmed = true,
Id = Guid.NewGuid().ToString(),
SecurityStamp = Guid.NewGuid().ToString()
}).ToArray();
var role = new IdentityRole("Admins") { Id = Guid.NewGuid().ToString() };
var roleToUser = appUsers.Select(x => new IdentityUserRole<string> { RoleId = role.Id, UserId = x.Id });
builder.Entity<ApplicationUser>().HasData(appUsers);
builder.Entity<IdentityRole>().HasData(role);
builder.Entity<IdentityUserRole<string>>().HasData(roleToUser);
base.OnModelCreating(builder);
}
(await _userManager.FindByEmailAsync(email)) == new Data.ApplicationDbContext().Users.First(x => x.NormalizedEmail == email);
– Regulatevar email = info.Principal.FindFirstValue(ClaimTypes.Email);
is returning the email that is being used for external login. So have you created an user with that email please? – CybilApplicationDbContext()
code please? – CybilUsers
set just like you do. The only difference between your query and what the UserStore does would in line 1414 but this can be ignored since FindByEmail returnsnull
for you. – LunularUsers
set in the DbContext really associated with theUser
class registered with theIdentityUser
? I mean maybe the_userManager
is operating on a completely different db set or it operates on an InMemoryUserStore? – LunulardbContext.Users.Count()
with_userManager.Users.Count()
to see if they deviate? Or execute your query on the set managed by the UserManager:_userManager.Users.First(x => x.NormalizedEmail == email);
and check whether this yields a result. If not, then your dbset is different from what the UserManager operates on... – LunularCount()
? Much more than that!_userManager.Users.FirstOrDefault(x => x.NormalizedEmail == email)
return the desired user... i edit the question, thank! – RegulateData-Seed
, share us the code. – Kathykathye