[英]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 我一直试图使用ASP.NET Core Identity来检查数据库中的现有用户,并且当我在UserManager类上调用FindByEmailAsync或其他Find方法时,即使AspNetUsers DB表为空,它也会找到具有ID的实体
I've doublechecked my Startup.cs file, because I'm almost sure that has to be something with DI. 我已经仔细检查了Startup.cs文件,因为我几乎可以肯定DI一定是这样。 But I use this EntityManager in my service class and this class is being registered as Scoped.
但是我在服务类中使用了这个EntityManager,并且该类已注册为Scoped。
It doesn't matter what i type in parameter, always returns some entity: 我在参数中键入什么都无所谓,总是返回一些实体:
My Startup DI configurations: 我的启动DI配置:
My User (derives from IdentityUser): 我的用户(来自IdentityUser):
My service registration (where I use UserManager via DI): 我的服务注册(我通过DI使用UserManager的地方):
I expect these methods not to find any user entities, but for some reason it always returs non-null values. 我希望这些方法不会找到任何用户实体,但是由于某种原因,它总是会返回非null值。
Might be the Identity caching or DI problems? 可能是身份缓存或DI问题?
FindByEmailAsync
method returns a Task
, not a user. FindByEmailAsync
方法返回Task
而不是用户。 If you check the type of the userExists
variable, you will see that it's of type Task<>
. 如果检查
userExists
变量的类型,您将看到它的类型为Task<>
。 So, the method doesn't return a user with id 70, but a Task
with id 70. If you modify this line: 因此,该方法不会返回ID为70的用户,而是ID为70的
Task
。如果您修改以下行:
var userExists = _userManager.FindByEmailAsync("rasfafasf");
by adding await
: 通过添加
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). 您将获得任务的实际结果,即使用该电子邮件的用户(在您的情况下为
null
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.