简体   繁体   English

ASP.NET Core身份UserManager使用FindByEmailAsync查找不存在的用户

[英]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.

相关问题 Asp.net 核心标识方法 FindByEmailAsync 抛出异常 - Asp.net core identity method FindByEmailAsync throws exception 在Asp.Net核心标识中创建没有UserManager的用户 - Create user without UserManager in Asp.Net Core Identity 用户经理<User>在数据库 asp.net core Identity 中找不到现有用户 - UserManager<User> does not find a existing user in database asp.net core Identity 在ASP.net核心身份(UserManager和SignInManager)是否可以立即禁止用户? - In ASP.net core Identity (UserManager & SignInManager) is it possible to ban a user immediately? ASP.NET Core Identity UserManager 返回用户而不包括相关对象(延迟加载) - ASP.NET Core Identity UserManager returns user without including related objects (Lazy Loading) ASP.NET 核心标识 userManager.AddToRoleAsync() - 按 id 添加 - ASP.NET Core Identity userManager.AddToRoleAsync() - add by id ASP.NET Core Identity不会注入UserManager <ApplicationUser> - ASP.NET Core Identity does not inject UserManager<ApplicationUser> 通过 ASP.NET Identity 2 中的 UserManager.Update() 更新用户 - Updating user by UserManager.Update() in ASP.NET Identity 2 使用ASP.NET Identity UserManager进行事务 - Transactions with ASP.NET Identity UserManager ASP.net Identity UserManager 未验证密码 - ASP.net Identity UserManager not verifying passwords
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM