public ActionResult Login(string Email, string MatKhau) { using (DBNoiThat_User db_u = new DBNoiThat_User()) { if (db_u.TaiKhoans.Where(p => p.Email == Email).FirstOrDefault() == null) { return(Json(new { success = false, message = "Email không tồn tại!" }, JsonRequestBehavior.AllowGet)); } NoiThat_v2._0.Models.TaiKhoan tk = db_u.TaiKhoans.Where(p => p.Email == Email).FirstOrDefault(); MatKhau = GetMD5(MatKhau + tk.Salt.ToString()); if (MatKhau != tk.MatKhau) { return(Json(new { success = false, message = "Mật khẩu không đúng!" }, JsonRequestBehavior.AllowGet)); } if (tk.IDQuyen == 1) { Session.Add("admin", tk); return(Json(new { admin = true }, JsonRequestBehavior.AllowGet)); } else { Session.Add("user", tk); } return(Json(new { user = true }, JsonRequestBehavior.AllowGet)); } }
public ActionResult Sign_up(NoiThat_v2._0.Models.TaiKhoan tk) { using (DBNoiThat_User db_u = new DBNoiThat_User()) { if (db_u.TaiKhoans.Where(p => p.Email == tk.Email).FirstOrDefault() != null) { return(Json(new { success = false, message = "Email đã tồn tại! Hãy thử đăng ký với một email khác!" }, JsonRequestBehavior.AllowGet)); } Random r = new Random(); tk.Salt = r.Next(100, 1000); tk.MatKhau = GetMD5(tk.MatKhau + tk.Salt.ToString()); tk.XacNhanMatKhau = tk.MatKhau; tk.IDQuyen = 2; db_u.TaiKhoans.Add(tk); db_u.SaveChanges(); tk = db_u.TaiKhoans.Where(p => p.Email == tk.Email).FirstOrDefault(); Session.Add("user", tk); return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); } }
public ActionResult AddDonHang(DonHang dh, string AddTaiKhoan) { using (DBNoiThat db = new DBNoiThat()) { try { dh.ThoiGian = DateTime.Now.ToString("dd/MM/yyyy"); dh.TinhTrangThanhToan = 0; dh.TinhTrangGiaoHang = 0; ThanhToan tt = (ThanhToan)Session["thanhtoan"]; dh.TongTien = tt.CanThanhToan; dh.DaGiamGia = tt.GiamGia; db.DonHangs.Add(dh); db.SaveChanges(); foreach (SanPhamCart item in (List <SanPhamCart>)Session["cart"]) { ChiTietDonHang ct = new ChiTietDonHang(); ct.IDSanPham = item.ID; ct.IDDonHang = db.DonHangs.OrderByDescending(p => p.ID).FirstOrDefault().ID; ct.SoLuong = item.SoLuong; ct.DonGia = item.Gia; ct.ThanhTien = item.SoLuong * item.Gia; db.ChiTietDonHangs.Add(ct); db.SaveChanges(); } if (AddTaiKhoan == "Add" && db.TaiKhoans.Where(p => p.Email == dh.Email).FirstOrDefault() == null) { DBNoiThat_User db_u = new DBNoiThat_User(); NoiThat_v2._0.Models.TaiKhoan tk = new NoiThat_v2._0.Models.TaiKhoan(); tk.Email = dh.Email; tk.HoTen = dh.HoTen; tk.DiaChi = dh.DiaChiGiaoHang; tk.Sdt = dh.Sdt; Random r = new Random(); tk.Salt = r.Next(100, 1000); tk.MatKhau = GetMD5(dh.Sdt + tk.Salt.ToString()); tk.XacNhanMatKhau = tk.MatKhau; tk.IDQuyen = 2; db_u.TaiKhoans.Add(tk); db.SaveChanges(); tk = db_u.TaiKhoans.Where(p => p.Email == tk.Email).FirstOrDefault(); Session.Add("user", tk); } Session.Remove("cart"); Session.Remove("thanhtoan"); return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); } catch { return(Json(new { success = false }, JsonRequestBehavior.AllowGet)); } } }