asp.net core Login function error "not all code paths return a value"

Viewed 212

Hello guys I am trying to make this Login function. I am getting an error on the Login() method, not all code paths return a value.

My action method is given below:

[HttpPost]
        public async Task<IActionResult> Login(LoginViewModel Input)
        {
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
                if (result.Succeeded)
                {
                    logger.LogInformation("User logged in."); 
                    return RedirectToAction("index", "home");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return RedirectToAction("Login", "User");
                }
            }
        }

I have added 2 return statements. I can't find my mistake. Can you help me fix this problem?

Thank you.

2 Answers

There are two if statements. One of the statements has an else also. So in total there are three possible execution paths. If the first if statement isn't true then it entirely skips the next one which has all the return paths. Hence it gets to the bottom of the function without a return value.

Try this instead.

    [HttpPost]
    public async Task<IActionResult> Login(LoginViewModel Input)
    {
        if (ModelState.IsValid)
        {
            var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
            if (result.Succeeded)
            {
                logger.LogInformation("User logged in."); 

                // Login is successful here, so we return now and the execution stops, meaning the bottom code never runs.
                return RedirectToAction("index", "home");
            }
        }

        // If we get to this line, either the MoxelState isn't valid or the login failed.
        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
        return RedirectToAction("Login", "User");
    }

First if statement check the model state validation but you are totally ignoring the falling of validation and missing a return for this function. you should add return view() or whatever suits your situation after your first modelstate.isvalid if statement. i added as comment.

 public async Task<IActionResult> Login(LoginViewModel Input)
        {
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
                if (result.Succeeded)
                {
                    logger.LogInformation("User logged in."); 
                    return RedirectToAction("index", "home");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return RedirectToAction("Login", "User");
                }
            }
//you must return something here
//return View(Input);
        }

Related