ASP.NET Core Identity UserManager 使用 FindByEmailAsync 查找不存在的用户
ASP.NET Core Identity UserManager Finds non-existant user with FindByEmailAsync
我一直在尝试使用 ASP.NET Core Identity 来检查我的数据库中的现有用户,当我在 UserManager class 上调用 FindByEmailAsync 或其他 Find 方法时,它找到了具有 Id 的实体即使 AspNetUsers DB table 为空
我仔细检查了我的 Startup.cs 文件,因为我几乎可以肯定这一定是 DI 的东西。但是我在我的服务 class 中使用了这个 EntityManager,并且这个 class 被注册为 Scoped。
不管我输入什么参数,总是returns一些实体:
我的启动 DI 配置:
我的用户(源自 IdentityUser):
我的服务注册(我通过 DI 使用 UserManager):
我希望这些方法不会找到任何用户实体,但由于某种原因它总是返回非空值。
可能是身份缓存或 DI 问题?
FindByEmailAsync
方法 return 是 Task
,不是用户。如果您检查 userExists
变量的类型,您将看到它是 Task<>
类型。因此,该方法不是 return id 70 的用户,而是 Task
id 70 的用户。如果您修改此行:
var userExists = _userManager.FindByEmailAsync("rasfafasf");
通过添加 await
:
var userExists = await _userManager.FindByEmailAsync("rasfafasf");
您将获得任务的实际结果,即拥有该电子邮件的用户(null
在您的情况下)。
我一直在尝试使用 ASP.NET Core Identity 来检查我的数据库中的现有用户,当我在 UserManager class 上调用 FindByEmailAsync 或其他 Find 方法时,它找到了具有 Id 的实体即使 AspNetUsers DB table 为空
我仔细检查了我的 Startup.cs 文件,因为我几乎可以肯定这一定是 DI 的东西。但是我在我的服务 class 中使用了这个 EntityManager,并且这个 class 被注册为 Scoped。
不管我输入什么参数,总是returns一些实体:
我的启动 DI 配置:
我的用户(源自 IdentityUser):
我的服务注册(我通过 DI 使用 UserManager):
我希望这些方法不会找到任何用户实体,但由于某种原因它总是返回非空值。
可能是身份缓存或 DI 问题?
FindByEmailAsync
方法 return 是 Task
,不是用户。如果您检查 userExists
变量的类型,您将看到它是 Task<>
类型。因此,该方法不是 return id 70 的用户,而是 Task
id 70 的用户。如果您修改此行:
var userExists = _userManager.FindByEmailAsync("rasfafasf");
通过添加 await
:
var userExists = await _userManager.FindByEmailAsync("rasfafasf");
您将获得任务的实际结果,即拥有该电子邮件的用户(null
在您的情况下)。