简体   繁体   English

ASP.NET 'FindByNameAsync' 返回 null?

[英]ASP.NET 'FindByNameAsync' returns null?

I'm having an issue with the ForgotPassword method for the base asp.net identity.我在使用基本 asp.net 身份的ForgotPassword方法时遇到问题。 When stepping through the code, the line var user = await UserManager.FindByNameAsync(model.Email);单步执行代码时,行var user = await UserManager.FindByNameAsync(model.Email); returns null , even though I have confirmed that the email address for the user exists in the aspnetusers table.返回null ,即使我已经确认用户的电子邮件地址存在于aspnetusers表中。 I'm not sure why Visual Studio will not allow me to step into the FindByNameAsync method?我不确定为什么 Visual Studio 不允许我进入FindByNameAsync方法? Not sure what's going on here?不知道这里发生了什么?

public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
            // Don't reveal that the user does not exist or is not confirmed
            return View("ForgotPasswordConfirmation");
        }

        var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
        var callbackUrl = Url.Action("ResetPassword", "Account", 
        new { UserId = user.Id, code = code }, protocol: Request.Url.Scheme);
        await UserManager.SendEmailAsync(user.Id, "Reset Password", 
        "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");        
        return View("ForgotPasswordConfirmation");
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

You are trying to find an user by an email address.您正在尝试通过电子邮件地址查找用户。

You should use UserManager.FindByEmailAsync您应该使用UserManager.FindByEmailAsync

This usually happens when you create the user using some other method than CreateAsync in Microsoft.AspNetCore.Identity.UserManager .当您使用Microsoft.AspNetCore.Identity.UserManager 中的CreateAsync以外的其他方法创建用户时,通常会发生这种情况。 I had the same issue because I was creating the users directly through EF, not the referred method.我遇到了同样的问题,因为我是直接通过 EF 创建用户的,而不是引用的方法。

All FindBy methods should work properly using this approach.所有 FindBy 方法都应该使用这种方法正常工作。

I had a similar issue for the project based on ASP.NET Core 2.2.我有一个基于 ASP.NET Core 2.2 的项目的类似问题。 Maybe my solution will be useful for someone.也许我的解决方案对某人有用。

The user can change their UserName in the UserProfile component (by default, the UserName was the same as Email, ie, user1@mail.com ).用户可以在 UserProfile 组件中更改他们的UserName (默认情况下, UserName与电子邮件相同,即user1@mail.com )。 If the user changed their Username in the profile from the default user1@mail.com to user1 , then they could not log in using this new UserName, only Email.如果用户将配置文件中的用户名从默认的user1@mail.comuser1 ,则他们无法使用此新用户名登录,只能使用电子邮件。

The line below always returned NULL.下面的行总是返回 NULL。

var user = await _userManager.FindByNameAsync(request.UserName);

After investigating the AspCore repository, I found FindByNameAsync method .在调查了 AspCore 存储库后,我找到了FindByNameAsync 方法 I become suspicious about NormalizeName line.我对NormalizeName行产生怀疑。 And my current model for the UserProfile model had only UserName property, which was mapped later using Automapper and saved to the database.而我当前的UserProfile模型只有UserName属性,稍后使用Automapper 映射并保存到数据库中。 So I added computed NormalizedUserName property and also mapped it with Automapper ( _mapper.Map(UserProfileModel, dbUser); ) and saved it to the database.因此,我添加了计算出的NormalizedUserName属性,并使用_mapper.Map(UserProfileModel, dbUser); ) 将其映射并将其保存到数据库中。

        public string NormalizedUserName
        {
            get
            {
                return UserName.ToUpper().Normalize(); // `UserManager` UserFindByNameAsync method is using `normalizedName` = `NormalizedUserName` from Users table (for some reason UPPERCASE, maybe SQL performance), otherwise we will always get NULL
            }
        }

Changes mentioned above solved my issue for NULL when using the FindByNameAsync method.使用FindByNameAsync方法时,上述更改解决了我的 NULL 问题。

当用户表应用了查询过滤器并且不满足过滤条件时,就会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM