Exemplo n.º 1
0
        public ActionResult Index(TaiKhoan taikhoan, string chkSuaLoi)
        {
            try
            {
                ViewBag.chkSuaLoi = chkSuaLoi;

                if (taikhoan.Email != null && taikhoan.MatKhau != null)
                {
                    DataProvider dp = new DataProvider();

                    using (MD5 md5hash = MD5.Create())
                    {
                        string hash = GetMd5Hash(md5hash, taikhoan.MatKhau);
                        taikhoan.MatKhau = hash;
                    }

                    SqlParameter[] param = new SqlParameter[2];

                    param[0] = new SqlParameter("@email", SqlDbType.NVarChar);
                    param[0].Value = taikhoan.Email;

                    param[1] = new SqlParameter("@matkhau", SqlDbType.NVarChar);
                    param[1].Value = taikhoan.MatKhau;

                    DataTable dt = null;

                    if (chkSuaLoi != null)
                    {
                        //Set level = Repeatable Read để giải quyết Unrepeatable Read
                        dt = dp.ExecuteProcQuery("sp_DangNhapTaiKhoan_Fixed", ref param);
                    }
                    else
                    {
                        //Set level = ReadCommitted mức mặc định
                        dt = dp.ExecuteProcQuery("sp_DangNhapTaiKhoan", ref param);
                    }

                    if (dt.Rows.Count > 0)
                    {
                        ViewBag.Result = true;
                        ViewBag.ErrorMessage = "Đăng nhập thành công";

                        TaiKhoan tk = new TaiKhoan();
                        tk.MaTaiKhoan = (int)dt.Rows[0]["mataikhoan"];
                        tk.Email = (string)dt.Rows[0]["email"];
                        tk.Ten = (string)dt.Rows[0]["ten"];
                        if (dt.Rows[0]["ngaysinh"] != DBNull.Value) tk.NgaySinh = (DateTime)dt.Rows[0]["ngaysinh"];
                        if (dt.Rows[0]["diachi"] != DBNull.Value) tk.DiaChi = (string)dt.Rows[0]["diachi"];
                        if (dt.Rows[0]["dienthoai"] != DBNull.Value) tk.DienThoai = (string)dt.Rows[0]["dienthoai"];
                        tk.MaLoaiTaiKhoan = (int)dt.Rows[0]["maloaitaikhoan"];
                        switch (tk.MaLoaiTaiKhoan)
                        {
                            case 1:
                                tk.LoaiTaiKhoan = LoaiTaiKhoan.Admin;
                                Session.Add("taikhoan", tk);
                                return Redirect("/Admin/CanHo");
                            case 2:
                                tk.LoaiTaiKhoan = LoaiTaiKhoan.Member;
                                Session.Add("taikhoan", tk);
                                break;
                            case 3:
                                tk.LoaiTaiKhoan = LoaiTaiKhoan.Sales;
                                Session.Add("taikhoan", tk);
                                return Redirect("/Admin/");
                        };

                        switch ((int)dt.Rows[0]["trangthai"])
                        {
                            case 0:
                                tk.TrangThai = TrangThaiTaiKhoan.Deactive;
                                break;
                            case 1:
                                tk.TrangThai = TrangThaiTaiKhoan.Active;
                                break;
                        }
                    }
                    else
                    {
                        ViewBag.Result = false;
                        ViewBag.ErrorMessage = "Đăng nhập thất bại";
                    }

                }
            }
            catch (Exception ex)
            {
                ViewBag.Result = false;
                ViewBag.ErrorMessage = ex.Message;
            }
            return View("~/Views/Login/DangNhap.cshtml");
        }