0

I have a strange issue happening while creating this MVC project. Yesterday I was able to log in as a user, it was authenticated and everything worked fine. Today for some reason the user is getting a successful login and when I try to redirect the user to my Index view in my Parts controller I get returned directly to the log in page.

Login

[AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = @"~\Views\Parts\Index";
        return View();
    }

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        var user = _userRepo.GetUserDetailsByEmail(model.Email);
        if(user == null)
        {
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
        }
        if (!ModelState.IsValid || !user.Active)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl); //Get to here
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

I checked User.Identity.IsAuthenticated and it returned false even after the sign in is successful. What am I doing wrong here? I have forms authentication enabled as well.

My Parts controller is decorated with the [Authorize] attribute. Which I believe is why the user is being redirected to log in (since the user is never authenticated).

EDIT

I noticed that the .ASPXAUTH cookie is not going through anymore. Could this be due to a bug or is there some steps I can take to fix that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Selthien
  • 1,178
  • 9
  • 33
  • You haven't told the system that the use is now logged in. See https://stackoverflow.com/questions/31584506/how-to-implement-custom-authentication-in-asp-net-mvc-5 – Michael Jun 11 '20 at 19:57
  • I am not using any custom authentication. I am using the default MVC template which worked perfectly fine as of yesterday. There was no extra steps to authenticate a user. – Selthien Jun 11 '20 at 20:05
  • The solution ended up being this: https://stackoverflow.com/questions/37006238/asp-net-core-auth-cookie-not-being-set-in-google-chrome-when-running-in-dev – Selthien Jun 11 '20 at 20:28

1 Answers1

0

try this:

case SignInStatus.Success:
    return RedirectToLocal(returnUrl, false); //Get to here
Syscall
  • 19,327
  • 10
  • 37
  • 52