示例#1
0
        public JsonResult QuenMatKhau(string diaChiEmail)
        {
            int result = 0;

            tkB = new TaiKhoanBusiness();
            if (ModelState.IsValid)
            {
                List <TaiKhoan> dsTK = tkB.TaiKhoanDaTonTai(diaChiEmail);
                if (dsTK.Count == 0) //Tài khoản chưa tồn tài
                {
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }
                else if (dsTK.Count != 0 && dsTK[0].MatKhau == "")// Tài khoản đã tồn tại, nhưng đăng nhập facebook
                {
                    result = 1;
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }
                result = 2;//Tài khoản đã tồn tại
                string matKhauRandom = RandomString(5);
                string noiDungMail   = "<p> Mật khẩu của bạn đã được đổi thành </p> " + matKhauRandom;
                //Gửi mail
                GuiMail(diaChiEmail, "Đổi mật khẩu", noiDungMail);

                //Cập nhật lại mật khẩu
                string mkMoiHashed;
                using (MD5 md5hash = MD5.Create())
                {
                    mkMoiHashed = GetMd5Hash(md5hash, matKhauRandom);
                }
                dsTK[0].MatKhau = mkMoiHashed;
                tkB.TaiKhoan_Update(dsTK[0]);
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
示例#2
0
        public ActionResult FacebookCallback(string code)
        {
            //var url= Request.Url;
            //Nếu code = null <==> Người dùng Cancel
            if (code == null)
            {
                return(RedirectToAction("DangNhapTaiKhoan", "Home"));
            }

            //Nếu người dùng đồng ý <=> code !=null
            var     fb     = new FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id     = ConfigurationManager.AppSettings["FbAppId"],
                client_secret = ConfigurationManager.AppSettings["FbAppSecret"],
                redirect_uri  = "http://localhost:61543/Home/FacebookCallback",
                code          = code
            });

            //Session["AccessToken"] = result.access_token;
            //Gán Access Token
            fb.AccessToken = result.access_token;
            //Các thông tin lấy về
            dynamic me         = fb.Get("me?fields=first_name,middle_name,last_name,id,email");
            string  email      = me.email;
            string  firstName  = me.first_name;
            string  middleName = me.middle_name;
            string  lastName   = me.last_name;


            tkB = new TaiKhoanBusiness();
            int id;

            //Lấy tài khoản trùng với email
            List <TaiKhoan> dsTaiKhoan = tkB.TaiKhoanDaTonTai(email);

            //Nếu Email tài khoản chưa tồn tại thì tạo mới tài khoản
            if (dsTaiKhoan.Count != 1)
            {
                TaiKhoan tk = new TaiKhoan();
                tk.Email    = email;
                tk.HoTenLot = middleName + " " + lastName;
                tk.Ten      = firstName;
                tk.MatKhau  = null;
                tk.IdQuyen  = 1;
                id          = tkB.TaiKhoan_InsertOutputId(tk);
            }
            else //Nếu Email đã tồn tại
            {
                id        = dsTaiKhoan[0].IdTaiKhoan;
                email     = dsTaiKhoan[0].Email;
                firstName = dsTaiKhoan[0].Ten;
            }

            Session["Id"]    = id;
            Session["Email"] = email;
            Session["Ten"]   = firstName;
            return(RedirectToAction("Index", "Home"));
        }
        /// <summary>
        /// Kiểm tra tên tài khoản đã tồn tại trong database hay chưa
        /// </summary>
        /// <param name="EmailDK"></param>
        /// <returns></returns>
        public JsonResult IsAlreadyExistEmail(string EmailDK)
        {
            tkB = new TaiKhoanBusiness();
            List <TaiKhoan> dsTK = tkB.TaiKhoanDaTonTai(EmailDK);

            if (dsTK.Count > 0)
            {
                return(Json(false, JsonRequestBehavior.AllowGet));
            }
            return(Json(true, JsonRequestBehavior.AllowGet));
        }
示例#4
0
        public ActionResult DangNhapTaiKhoan(TaiKhoanDangNhap tkdn)
        {
            tkB = new TaiKhoanBusiness();
            if (ModelState.IsValid)
            {
                //Kiếm tra mail tồn tại
                List <TaiKhoan> dsTK = tkB.TaiKhoanDaTonTai(tkdn.Email);
                if (dsTK.Count > 0)//Nếu tài khoản đã tồn tại
                {
                    string hashedPassword;
                    using (MD5 md5hash = MD5.Create())
                    {
                        hashedPassword = GetMd5Hash(md5hash, tkdn.MatKhau);
                    }
                    if (hashedPassword == dsTK[0].MatKhau)// So sánh mật khẩu, nếu đúng thì gán Session
                    {
                        qB = new QuyenBusiness();
                        int    quyenID  = dsTK[0].IdQuyen;
                        string tenQuyen = qB.Quyen_GetNameById(quyenID);

                        Session["Id"]    = dsTK[0].IdTaiKhoan;
                        Session["Email"] = dsTK[0].Email;
                        Session["Ten"]   = dsTK[0].Ten;
                        Session["Quyen"] = tenQuyen;
                        Session.Timeout  = 20;
                        return(RedirectToAction("Index", "Home"));
                    }
                    else//Nếu sai mật khẩu
                    {
                        ModelState.AddModelError("LoiDangNhap", "Mật khẩu không đúng");
                        ViewBag.tab = "DangNhap";
                        return(View("DangNhap_DangKyTaiKhoan"));
                    }
                }
                else//Nếu Email không đúng => tài khoản không tồn tại => báo lôi
                {
                    ModelState.AddModelError("LoiDangNhap", "Địa chỉ Email không đúng");
                }
            }
            //Active tab
            ViewBag.tab = "DangNhap";
            return(View("DangNhap_DangKyTaiKhoan"));
        }