public ActionResult Buy(int id) { var cookie = HelperCookie.GetFromCookie("shop-art", "shop-art-key"); Cart cart = new Cart { CartDate = DateTime.Now, Cookie = cookie, ItemCount = 1, }; this.CheckAuditPattern(cart, true); CartItem item = new CartItem { Price = 100, ProductId = id, Quantity = 1 }; this.CheckAuditPattern(item, true); cart.CartItem = new List <CartItem>() { item }; db.Cart.Add(cart); db.SaveChanges(); return(RedirectToAction("Index", "CartItem")); }
protected void Session_Start(object sender, EventArgs e) { var cookie = HelperCookie.CookieExist("shop-art", "shop-art-key"); if (!cookie) { HelperCookie.StoreInCookie("shop-art", "shop-art-key", "shop-art-2020", new DateTime(2020, 8, 1)); } }
public void ProcessRequest(HttpContext context) { // 从会话状态集合中移除所有的键和值 context.Session.Clear(); // 取消当前会话 context.Session.Abandon(); // 清除cookie里的登录信息 HelperCookie.removeCookie("AdminLogin"); context.Response.Redirect("/BackManager/login.aspx"); //跳转登录页 }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 首先从cookie中读取保存的用户名密码 string strCKName = "AdminLogin"; string strUsername = HelperCookie.getCookie(strCKName, "username"); string strPassword = HelperCookie.getCookie(strCKName, "password"); if (!"".Equals(strUsername) && !"".Equals(strPassword)) { int intAdminID; string strPurviews; int intEnabled; int intIsDeleted; BllAdmin.login(strUsername, strPassword, out intAdminID, out strPurviews, out intEnabled, out intIsDeleted); if (intAdminID > 0) { if (intEnabled == 1 && intIsDeleted == 0) { Session["AdminID"] = intAdminID; Session["Purviews"] = strPurviews; Response.Redirect("home.aspx"); } else { // 清除 Cookie HelperCookie.removeCookie("AdminLogin"); } } } // 到了这里,说明cookie中没有保存账号密码,或者保存的账号密码不能登录 tbUsername.Value = ""; tbPassword.Value = ""; cbRememberMe.Checked = false; } }
protected void btnLogin_Click(object sender, EventArgs e) { string strMsgError = ""; string strUsername = tbUsername.Value.ToString().Trim(); if ("".Equals(strUsername)) { strMsgError += "用户名不能为空!\n"; } string strPassword = tbPassword.Value.ToString(); if ("".Equals(strPassword)) { strMsgError += "密码不能为空!\n"; } if (!"".Equals(strMsgError)) { HelperUtility.showAlert(strMsgError, "login.aspx"); } // 验证完毕,提交数据 strPassword = HelperCrypto.encode(strPassword, "DES"); int intAdminID; string strPurviews; int intEnabled; int intIsDeleted; BllAdmin.login(strUsername, strPassword, out intAdminID, out strPurviews, out intEnabled, out intIsDeleted); if (intAdminID > 0) { if (intEnabled == 1 && intIsDeleted == 0) { // 用户名密码验证正确,保存到cookie里 string strCKName = "AdminLogin"; if (cbRememberMe.Checked) { string strCKKey = "username"; string strCKValue = strUsername; HelperCookie.setCookie(strCKName, strCKKey, strCKValue, 7 * 24 * 60); strCKKey = "password"; strCKValue = strPassword; HelperCookie.setCookie(strCKName, strCKKey, strCKValue, 7 * 24 * 60); } else { // 如果没有勾选记住我复选框,就要清除cookie里的登录数据 // 将过期时间设置为-1即可清除保存的值 HelperCookie.removeCookie(strCKName); } // 保存到Session里 Session["AdminID"] = intAdminID; Session["Purviews"] = strPurviews; Response.Redirect("home.aspx"); } else { string strOPMsg = "<script>"; strOPMsg += "alert('登录失败!此用户已暂停使用!请联系网络管理员!');"; strOPMsg += "location='login.aspx';</script>"; Response.Write(strOPMsg); } } else { string strOPMsg = "<script>"; strOPMsg += "alert('登录失败!用户名密码不正确,请重新输入!');"; strOPMsg += "location='login.aspx';</script>"; Response.Write(strOPMsg); } }