ASP.NET 'FindByNameAsync' returns null?

10,195

Solution 1

You are trying to find an user by an email address.

You should use UserManager.FindByEmailAsync

Solution 2

This usually happens when you create the user using some other method than CreateAsync in Microsoft.AspNetCore.Identity.UserManager. I had the same issue because I was creating the users directly through EF, not the referred method.

All FindBy methods should work properly using this approach.

Solution 3

I had a similar issue for the project based on 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, i.e., [email protected]). If the user changed their Username in the profile from the default [email protected] to user1, then they could not log in using this new UserName, only Email.

The line below always returned NULL.

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

After investigating the AspCore repository, I found FindByNameAsync method. I become suspicious about NormalizeName line. And my current model for the UserProfile model had only UserName property, which was mapped later using Automapper and saved to the database. So I added computed NormalizedUserName property and also mapped it with Automapper (_mapper.Map(UserProfileModel, dbUser);) and saved it to the database.

        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.

Share:
10,195
PixelPaul
Author by

PixelPaul

Updated on August 03, 2022

Comments

  • PixelPaul
    PixelPaul almost 2 years

    I'm having an issue with the ForgotPassword method for the base asp.net identity. When stepping through the code, the line 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. I'm not sure why Visual Studio will not allow me to step into the FindByNameAsync method? 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);
    }