public ActionResult LoginPage(JooleApp.Domain.tblUser userModel, HttpPostedFileBase inputFile) { if (ModelState.IsValid) { var userDetails = service.GetUserByName(userModel.userName).FirstOrDefault(); if (inputFile != null) { string ImageName = System.IO.Path.GetFileName(inputFile.FileName); string physicalPath = Server.MapPath("~/Images/" + ImageName); // save image in folder inputFile.SaveAs(physicalPath); userModel.userImage = ImageName; ViewBag.ImageSrc = "/Images/" + ImageName; } if (userDetails == null) { service.Insert(userModel); ViewBag.RegisterMessage = "Success! Please back to login."; return(View(userModel)); } else { ViewBag.RegisterFailMessage = "User name already exists"; } ViewBag.HasInput = "true"; } return(View()); }
public ActionResult Authorize(JooleApp.Domain.tblUser userModel) { HttpCookie cookie = new HttpCookie("tblJooleUser"); var userDetails = service.GetAll().FirstOrDefault(); //user name login if (userModel.userName != null) { userDetails = service.GetUserNameAuth(userModel.userName, userModel.password).FirstOrDefault(); } //email login if (userDetails == null) { userDetails = service.GetUserEmailAuth(userModel.userName, userModel.password).FirstOrDefault(); } if (userDetails == null) { //login failed ViewBag.LoginErrorMessage = "Wrong Login ID or Password."; return(View("LoginPage")); } else { //login success System.Web.Security.FormsAuthentication.SetAuthCookie(userModel.userName, false); System.Web.Security.FormsAuthentication.SetAuthCookie(userModel.emailAddress, false); if (String.IsNullOrEmpty(userDetails.userImage)) { //set default userImage Session["UserAvatar"] = "http://via.placeholder.com/150x150"; } else { Session["UserAvatar"] = "/Images/" + userDetails.userImage; } Session["userID"] = userDetails.userID; Session["userName"] = userDetails.userName; Session["emailAddress"] = userDetails.emailAddress; //if (userModel.RememberMe) //{ // //cookie.Values.Add("LoginID", userDetails.LoginID); // cookie["loginID"] = userModel.UserID; // cookie.Expires = DateTime.Now.AddDays(15); // HttpContext.Response.Cookies.Add(cookie); //} return(RedirectToAction("SearchPage", "Search")); //action name, controller name } }