Search code examples
asp.netasp.net-core-mvcasp.net-identity

Cannot login on ASP.NET Identity site


I am developing a new ASP.NET MVC Core 2.0 and I'm using ASP.NET Identity for the membership system of the site. However, I cannot log in the user even though my SignInManager returns SignInStatus.Success after the signin attempt.

Here is my controller code for the login function:

public async Task<IActionResult> Login(LoginViewModel model)
    {
        var user = _db.Users.Where(x => x.Email == model.Email).FirstOrDefault();

        if (!ModelState.IsValid)
            return View();

        var result = await _signinManager.PasswordSignInAsync(
            model.Email, model.Password,
            model.RememberMe, false
            );

        if (!result.Succeeded)
        {
            ModelState.AddModelError("", "Login error");
            return View();
        }

        if (user.Role == "Admin")
        {
            return RedirectToAction("Users");
        }

        return RedirectToAction("Account");
    }

Solution

  • Ensure you have Identity services and middleware is setup in Startup.cs file.

    Ensure you have app.UseAuthentication in Configure method. And Cookie based authentication is setup in ConfigureServices method using services.ConfigureApplicationCookie

    Check article