public void Login() { UserLoginModel userLoginModel = new UserLoginModel() { C_UserName = "******", C_UserPassword = "******" }; var mockControllerContext = new Mock<ControllerContext>(); var mockSession = new Mock<HttpSessionStateBase>(); mockSession.Setup(s => s["LoggedUserID"]).Returns("3"); //somevalue mockSession.Setup(s => s["LoggedUserName"]).Returns("Krutika"); mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object); userController.ControllerContext = mockControllerContext.Object; var redirectToResult = userController.Login(userLoginModel) as RedirectToRouteResult; Assert.AreEqual("List", redirectToResult.RouteValues["action"]); Assert.AreEqual("User", redirectToResult.RouteValues["controller"]); }
public ActionResult Login(UserLoginModel val) { try { var model = val; //Validate the user ContinuousIntegrationEntities ci = new ContinuousIntegrationEntities(); var user = (from a in ci.T_Registrations where a.C_FirstName == val.C_UserName && a.C_Password == val.C_UserPassword select a).FirstOrDefault(); if (user==null) { ModelState.AddModelError("", "Invalid name and password"); return View("Login"); } Session["LoggedUserID"] = user.C_RegisterID.ToString(); Session["LoggedUserName"] = user.C_FirstName.ToString(); return RedirectToAction("List", "User"); } catch (Exception e) { throw new ApplicationException("Error: " +e); } }