0

I'm starter in .NET MVC. I want to pass a model from one controller to another controller, model contain a password. My first controller is auth method, in that I used Claims, but another controller is a vote method there I need to Post an ID, Password and Vote. Password I need in vote controller for voting confirmation in database. My code: My model:

public class LoginModel
{
    public string IDNP { get; set; }
    public string VnPassword { get; set; 
}

My first controller:

public class AuthController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(LoginModel model)
    {

        var data = new LoginData();
        data.IDNP = model.IDNP;
        data.VnPassword = model.VnPassword;

        var response = await session.Login(data);
        if (response.Status == true)
        {
            var authclaims = new List<Claim>()
            {
                new Claim(ClaimTypes.Name, data.IDNP),
            };

            var authIdentity = new ClaimsIdentity(authclaims, "User Identity");

            var userPrincipal = new ClaimsPrincipal(new[] {authIdentity});
            HttpContext.SignInAsync(userPrincipal);

            return RedirectToAction("Index", "Vote",new{pass=data.VnPassword});
        }
        else
        {
            return View();
        }
    }
}

My second controller:

public class VoteController : Controller
{
    private IVote connection;
    public VoteController()
    {
        var bl = new BusinessManager();
        connection = bl.GetVote();
    }
    [Authorize]
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(VoteModel vote)
    {
        var identity = (ClaimsIdentity)User.Identity;
        var voteindex = new VoteData();
        voteindex.IDNP = identity.Name;
        voteindex.VnPassword = ;
        voteindex.Party = vote.Party;

        var response = await connection.Vote(voteindex);
        if (response.Status == true)
            return RedirectToAction("Index", "Home");
        else
        {

            return RedirectToAction("Index", "Auth");
        }
    }
}

1 Answers1

0

Actually, it's a bad idea to send Id and Password to another controller. It conflicts SRP rule. You should have only one controller that gets and uses password.

However, if you want to use this action, you can use this

Mehdi Isaloo
  • 174
  • 1
  • 4