public IActionResult Login([FromServices] CustValidation cv, string username, string hashPassword) { //no username stored in session state, request login and validation if (string.IsNullOrEmpty(username)) { return(View("Index")); } Customer customer = cv.GetCust(username); if (customer == null) { TempData["errmsg"] = "Username not found."; return(RedirectToAction("Index")); } bool pwdcheck = cv.PasswordCheck(customer, hashPassword); if (pwdcheck == false) { TempData["errmsg"] = "Incorrect password."; return(RedirectToAction("Index")); } //customer validated, store username in session, redirect to Home HttpContext.Session.SetString("username", username); HttpContext.Session.SetString("cartQty", "0"); return(RedirectToAction("Index", "Home")); }
public IActionResult Index(string id) { //initiate session for not logged in users if (HttpContext.Session.GetString("tempCart") == null) { HttpContext.Session.SetString("tempCart", ""); } //search view if (!String.IsNullOrEmpty(id)) { string searchString = id.ToLower(); List <Product> productsNew = new List <Product>(); var products = cv.Listofproduct(); var products2 = products.Where(p => p.ProductName.ToLower().Contains(searchString) || p.ProductDescription.ToLower().Contains(searchString)); foreach (Product product in products2) { productsNew.Add(product); } ViewData["ProductData"] = productsNew; ViewData["session"] = HttpContext.Session.GetString("username"); string username1 = (string)ViewData["session"]; Customer customer1 = cv.GetCust(username1); if (customer1 != null) { ViewData["CartQty"] = (cv.GetQtyInSessionCart(username1)).ToString(); ViewData["customername"] = customer1.FirstName; return(View()); } ViewData["CartQty"] = HttpContext.Session.GetString("cartQty"); return(View()); } ViewData["session"] = HttpContext.Session.GetString("username"); string username = (string)ViewData["session"]; Customer customer = cv.GetCust(username); if (customer != null) { ViewData["customername"] = customer.FirstName; ViewData["ProductData"] = cv.Listofproduct(); string tempCart = HttpContext.Session.GetString("tempCart"); if (tempCart != "") { string[] tempProductList = tempCart.Split(";"); List <Product> products = new List <Product>(); foreach (string tempProd in tempProductList) { Product product = cv.GetProd(tempProd); products.Add(product); } foreach (Product p in products) { ShoppingCart cart1 = cv.GetCart(username, p.ProductName); if (cart1 == null) { cv.CreateCart(username, p.ProductName); } else { cv.IncreaseProductQty(cart1); } } } HttpContext.Session.Remove("tempCart"); ViewData["CartQty"] = (cv.GetQtyInSessionCart(username)).ToString(); return(View()); } ViewData["ProductData"] = cv.Listofproduct(); ViewData["CartQty"] = HttpContext.Session.GetString("cartQty"); return(View()); }