Search code examples
c#asp.net-core-mvcasp.net-core-identity

Why can't I log in with the newly seeded power user (Asp.Net Core Identity)?


I have an Asp.Net Core 3.1 web app with Identity. I use this code to seed a power user to the database:

var poweruser = new ApplicationUser
{
    UserName = Configuration.GetSection("AppSettings")["UserName"],
    Email = Configuration.GetSection("AppSettings")["UserEmail"],
    FirstName = Configuration.GetSection("AppSettings")["UserFirstName"],
    LastName = Configuration.GetSection("AppSettings")["UserLastName"],
    PhoneNumber = Configuration.GetSection("AppSettings")["UserPhoneNumber"]
};

string userPassword = Configuration.GetSection("AppSettings")["UserPassword"];
var user = await UserManager.FindByEmailAsync(Configuration.GetSection("AppSettings")["UserEmail"]);

if (user == null)
{
    var createPowerUser = await UserManager.CreateAsync(poweruser, userPassword);
    if (createPowerUser.Succeeded)
    {
        await UserManager.AddToRoleAsync(poweruser, "SiteAdmin");
    }
}

In appsettings.json, the section "AppSettings" looks like this:

"AppSettings": {
  "UserName": "ab@cd.ef",
  "UserEmail": "ab@cd.ef",
  "UserFirstName": "Admin",
  "UserLastName": "Admin",
  "UserPhoneNumber":  "12345678",
  "UserPassword": "pa5sw&Rd"
}

The user is created in the database:

enter image description here

But when I try to log in, I get a failed login attempt. I have seen this and this similar post, but in those cases, the username and email address were not identical. In my case, they are (ab@cd.ef). I copy-pasted the password from appsettings.json to make sure I wasn't mistyping.

Update

This is the method receiving the login form (Login.cshtml.cs):

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    returnUrl = returnUrl ?? Url.Content("~/");

    if (ModelState.IsValid)
    {
        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, set lockoutOnFailure: true
        var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
        if (result.Succeeded)
        {
            _logger.LogInformation("User logged in.");
            return LocalRedirect(returnUrl);
        }
        if (result.RequiresTwoFactor)
        {
            return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
        }
        if (result.IsLockedOut)
        {
            _logger.LogWarning("User account locked out.");
            return RedirectToPage("./Lockout");
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            return Page();
        }
    }

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

Solution

  • Don't you hate it when the solution to a difficult problem turns out to be simple?

    In this case, all it took was to set PhoneNumberConfirmed to true, and I was able to log in...