public void SaveToCookie(ProductViewModel product, int quantity) { if (CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey) == null) { List <CartItemViewModel> cart = new List <CartItemViewModel>(); cart.Add(new CartItemViewModel { Product = product, Quantity = 1 }); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } else { List <CartItemViewModel> cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); int index = cart.FindIndex(x => x.Product.Id == product.Id); if (index != -1) { cart[index].Quantity += quantity; } else { cart.Add(new CartItemViewModel { Product = product, Quantity = quantity }); } CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } }
public void DeleteItemFromCookie(int productId) { List <CartItemViewModel> cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); int index = cart.FindIndex(x => x.Product.Id == productId); if (index != -1) { cart.RemoveAt(index); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } }
public void UpdateToCookie(int productId, int quantity) { List <CartItemViewModel> cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); int index = cart.FindIndex(x => x.Product.Id == productId); if (index != -1) { cart[index].Quantity = quantity; CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } }
public async Task <IActionResult> Order(OrderCreateRequest request) { request.OrderDetails = new List <OrderDetailCreateRequest>(); foreach (var item in ViewBag.cart) { var detail = new OrderDetailCreateRequest { ProductId = item.Product.Id, Price = item.Product.Price, Quantity = item.Quantity }; request.OrderDetails.Add(detail); } if (ModelState.IsValid) { var result = await _orderService.Create(request); if (result.IsSuccessed) { var data = new { Id = result.ResultObject, Name = request.ShipName, Phone = request.ShipPhone, Address = request.ShipAddress, Total = request.Total, Status = OrderStatus.InProgress.ToString(), Create_At = DateTime.Now.ToShortDateString(), }; await ChannelHelper.Trigger(data, "feed", "new_feed", _configuration); var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; List <CartItemViewModel> cart = new List <CartItemViewModel>(); if (ViewBag.CartId != null) { await _cartService.DeleteAll(Convert.ToInt32(ViewBag.CartId)); } //CookieHelpers.SetObjectAsJson(Response.Cookies, "CartId"); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); return(RedirectToAction("index", "home")); } ModelState.AddModelError(string.Empty, result.Message); return(View("index")); } else { return(View("index")); } }
public async Task <IActionResult> Index(LoginRequest request, [FromQuery] string ReturnUrl) { if (!ModelState.IsValid) { return(View(ModelState)); } var result = await _userAPIClient.Authenticate(request); if (result.IsSuccessed == false) { TempData["message"] = result.Message; ModelState.AddModelError("", result.Message); ViewBag.ErrorServerSide = true; return(View()); } TempData["Succes"] = "Login Succsess!"; CookieHelpers.SetObjectAsJson(Response.Cookies, "Token", result.ResultObject, 10); var userPrincipal = this.ValidateToken(result.ResultObject); var UserId = new Guid(userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); var cartResult = await _cartService.GetById(UserId); if (cartResult.IsSuccessed == true) { var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; CookieHelpers.RemoveCookie(Response.Cookies, CartSessionKey); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cartResult.ResultObject.CartItems, null); } var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), IsPersistent = true // có sử dụng persistent cookie }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authProperties); if (!string.IsNullOrEmpty(ReturnUrl)) { return(Redirect(ReturnUrl)); } else { return(RedirectToAction("index", "home")); } }
public async Task <IActionResult> ExternalLoginCallback(string returnUrl, string remoteError = null) { ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(); returnUrl = returnUrl ?? Url.Content("~/"); if (remoteError != null) { ModelState.AddModelError(string.Empty, $"Error from external provider:{remoteError}"); return(View("Index")); } var externalLoginRequest = new ExternalLoginRequest { FullName = info.Principal.FindFirstValue(ClaimTypes.Name), Email = info.Principal.FindFirstValue(ClaimTypes.Email), ProviderKey = info.ProviderKey, LoginProvider = info.LoginProvider, ProviderDisPlayName = info.ProviderDisplayName, ImagePath = info.Principal.Claims.FirstOrDefault(c => c.Type == "picture").Value, }; var result = await _userAPIClient.ExternalLoginCallback(externalLoginRequest); if (result.IsSuccessed == false) { TempData["message"] = result.Message; ModelState.AddModelError("", result.Message); ViewBag.ErrorServerSide = true; return(View()); } TempData["Succes"] = "Login Succsess!"; //HttpContext.Session.SetString("Token", result.ResultObject); CookieHelpers.SetObjectAsJson(Response.Cookies, "Token", result.ResultObject, 10); var userPrincipal = this.ValidateToken(result.ResultObject); var UserId = new Guid(userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); var cartResult = await _cartService.GetById(UserId); if (cartResult.IsSuccessed == true) { var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; CookieHelpers.RemoveCookie(Response.Cookies, CartSessionKey); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cartResult.ResultObject.CartItems, null); } var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), IsPersistent = false // có sử dụng persistent cookie }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal, authProperties); if (!string.IsNullOrEmpty(returnUrl)) { return(Redirect(returnUrl)); } else { return(RedirectToAction("index", "home")); } }