Kasparas Taminskas
Kasparas Taminskas

Reputation: 301

ASP.NET Core Identity UserManager Finds non-existant user with FindByEmailAsync

I've been trying to use ASP.NET Core Identity in order to check for existing users in my db and when I call FindByEmailAsync or other Find methods on UserManager class it finds entity with Id even though AspNetUsers DB table is empty

I've doublechecked my Startup.cs file, because I'm almost sure that has to be something with DI. But I use this EntityManager in my service class and this class is being registered as Scoped.

It doesn't matter what i type in parameter, always returns some entity:

Checking with any string

My Startup DI configurations:

My User (derives from IdentityUser): Checking with any string

My service registration (where I use UserManager via DI): Service container registration

I expect these methods not to find any user entities, but for some reason it always returs non-null values.

Might be the Identity caching or DI problems?

Upvotes: 1

Views: 1353

Answers (1)

Marko Papic
Marko Papic

Reputation: 1916

FindByEmailAsync method returns a Task, not a user. If you check the type of the userExists variable, you will see that it's of type Task<>. So, the method doesn't return a user with id 70, but a Task with id 70. If you modify this line:

var userExists = _userManager.FindByEmailAsync("rasfafasf");

by adding await:

var userExists = await _userManager.FindByEmailAsync("rasfafasf");

you will get an actual result of a Task, which is the user with that email (null in your case).

Upvotes: 4

Related Questions