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

ASP.NET MVC : calling a controller from another controller


I am writing a web application with ASP.NET MVC, and I would like to call a controller from another controller. More precisely I would like to call a routine in the Login controller, where I do sign in and sign out, from the controller where I allow the normal user to change his/her password. Thus, I check the old password (given by the user in the change password form).

I tried to search in the internet methods to change user (not Administrator) password but I didn't find anything good.

Thank you.


Solution

  • I didn´t called a controller from another controller, instead I called my User Administration Controller AdminController from a View within the HomeController controller (via na input button).

    Then the code of my routine EditByUser in the AdminController was written as:

            [HttpPost]
            [Authorize]
            public async Task<IActionResult> EditByUser(string email, string passwordnew, string passwordconf)
            {
                AppUser user = await userManager.FindByEmailAsync(email);
                if (user != null)
                {
                    IdentityResult validEmail
                        = await userValidator.ValidateAsync(userManager, user);
                    if (!validEmail.Succeeded)
                    {
                        AddErrorsFromResult(validEmail);
                    }                
    
                    IdentityResult validPassnew = null;
                    bool passNewEqPassConf = passwordconf == passwordnew;
                    if (!passNewEqPassConf)
                    {
                        ModelState.AddModelError("", "New Password not equal to Confirmation!");
                    }
                    if (!string.IsNullOrEmpty(passwordnew) && passNewEqPassConf)
                    {
                        validPassnew
                            = await passwordValidator.ValidateAsync(userManager, user, passwordnew);
                        if (validPassnew.Succeeded)
                        {
                            user.PasswordHash = passwordHasher.HashPassword(user, passwordnew);
                        }
                        else
                        {
                            AddErrorsFromResult(validPassnew);
                        }
                    }
                    if (((validEmail.Succeeded && validPassnew == null)
                        || (validEmail.Succeeded
                            && passwordnew != string.Empty && validPassnew.Succeeded)) && passNewEqPassConf)
                    {
                        IdentityResult result = await userManager.UpdateAsync(user);
                        if (result.Succeeded)
                        {
                            return RedirectToAction("Index", "Home", new { email = user.Email });
                        }
                        else
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "User not found!");
                }
    
                return View(user);
            }
    
    

    However, I didn't manage to check the old password… The sign in is made on another controller…