Ejemplo n.º 1
0
        public async Task <ActionResult> SMTP(SystemInformation model)
        {
            try
            {
                var systemInformation =
                    (await _informationRepository.All.FirstOrDefaultAsync());
                if (systemInformation == null)
                {
                    systemInformation = new SystemInformation
                    {
                        SMTPEmail    = model.SMTPEmail,
                        SMTPName     = model.SMTPName,
                        SMTPPassword = Base64Hepler.Base64Encode(model.SMTPPassword)
                    };
                    await _informationRepository.AddAsync(systemInformation);

                    await _informationRepository.SaveAsync(RequestContext);
                }
                else
                {
                    systemInformation.SMTPEmail    = model.SMTPEmail;
                    systemInformation.SMTPName     = model.SMTPName;
                    systemInformation.SMTPPassword = Base64Hepler.Base64Encode(model.SMTPPassword);
                    _informationRepository.UpdateAsync(systemInformation);
                    await _informationRepository.SaveAsync(RequestContext);
                }
                return(Json(new { success = true }));
            }
            catch (Exception ex)
            {
                string message = "Đã xảy ra lỗi: " + ex.Message;
                return(Json(new { success = false, message = message }));
            }
        }
Ejemplo n.º 2
0
 public async Task <ActionResult> SMTP(SystemInformation model)
 {
     try
     {
         var systemInformation = (await _repository.GetRepository <SystemInformation>().GetAllAsync()).FirstOrDefault();
         if (systemInformation == null)
         {
             systemInformation              = new SystemInformation();
             systemInformation.SMTPEmail    = model.SMTPEmail;
             systemInformation.SMTPName     = model.SMTPName;
             systemInformation.SMTPPassword = Base64Hepler.EncodeTo64UTF8(model.SMTPPassword);
             int result = await _repository.GetRepository <SystemInformation>().CreateAsync(systemInformation, AccountId);
         }
         else
         {
             systemInformation.SMTPEmail    = model.SMTPEmail;
             systemInformation.SMTPName     = model.SMTPName;
             systemInformation.SMTPPassword = Base64Hepler.EncodeTo64UTF8(model.SMTPPassword);
             int result = await _repository.GetRepository <SystemInformation>().UpdateAsync(systemInformation, AccountId);
         }
         _cacheFactory.SaveCache("SystemInformation", systemInformation);
         return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         string message = "Đã xảy ra lỗi: " + ex.Message;
         return(Json(new { success = false, message = message }, JsonRequestBehavior.AllowGet));
     }
 }
Ejemplo n.º 3
0
        public async Task <ActionResult> Index()
        {
            SystemInformation systemInformation =
                (await _informationRepository.All.FirstOrDefaultAsync() ?? new SystemInformation()
            {
                Id = 0,
                SMTPPassword = ""
            });

            systemInformation.SMTPPassword = Base64Hepler.Base64Decode(systemInformation.SMTPPassword);
            return(View(systemInformation));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Index()
        {
            SystemInformation systemInformation = (await _repository.GetRepository <SystemInformation>().GetAllAsync()).FirstOrDefault();

            if (systemInformation == null)
            {
                systemInformation = new SystemInformation()
                {
                    Id           = 0,
                    SMTPPassword = ""
                };
            }
            systemInformation.SMTPPassword = Base64Hepler.DecodeFrom64(systemInformation.SMTPPassword);
            try
            {
                string filePath = Server.MapPath("~/Uploads/images/chung/");
                if (!Directory.Exists(filePath))
                {
                    DirectoryInfo di = Directory.CreateDirectory(filePath);
                }
            }
            catch { }
            return(View(systemInformation));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> ConfirmPassword(string code)
        {
            string message;

            try
            {
                var forgetPassword = await _forgetPasswordRepository.All.FirstOrDefaultAsync(o => o.ActiveCode == code);

                if (forgetPassword == null)
                {
                    message = "Yêu cầu của bạn không chính xác. Vui lòng kiểm tra lại!";
                }
                else
                {
                    if (forgetPassword.Status != 0)
                    {
                        message = "Yêu cầu của bạn không chính xác. Vui lòng kiểm tra lại!";
                    }
                    else
                    {
                        DateTime requestTime = forgetPassword.RequestTime;
                        TimeSpan timespan    = DateTime.Now - requestTime;
                        double   hours       = timespan.TotalMinutes;
                        if (hours > 10)
                        {
                            var item = _forgetPasswordRepository.All.FirstOrDefault(x => x.AccountId == forgetPassword.AccountId);
                            if (item != null)
                            {
                                item.Status = 3;
                                _forgetPasswordRepository.UpdateAsync(item);
                                await _forgetPasswordRepository.SaveAsync();
                            }
                            await _forgetPasswordRepository.SaveAsync();

                            message = "Yêu cầu khôi phục mật khẩu của bạn đã hết thời hạn!";
                        }
                        else
                        {
                            var account = await _accountRepository.All.FirstOrDefaultAsync(x => x.Id == forgetPassword.AccountId.ToString());

                            var item1 = _forgetPasswordRepository.All.FirstOrDefault(x => x.AccountId == forgetPassword.AccountId);
                            if (item1 != null)
                            {
                                account.Password = StringHelper.stringToSHA512(Base64Hepler.Base64Decode(item1.TemporaryPassword));
                                _accountRepository.UpdateAsync(account);
                                await _forgetPasswordRepository.SaveAsync();

                                try
                                {
                                    item1.Status     = 1;
                                    item1.ActiveTime = DateTime.Now;
                                    _forgetPasswordRepository.UpdateAsync(item1);
                                    await _forgetPasswordRepository.SaveAsync();

                                    message =
                                        "Yêu cầu của bạn đã được xử lý thành công. Bạn đã có thể dùng mật khẩu mới để truy cập!";
                                    //Có cần gửi email thông báo là đổi mật khẩu thành công hay không?
                                }
                                catch
                                {
                                    var item2 = _forgetPasswordRepository.All.FirstOrDefault(x =>
                                                                                             x.AccountId == forgetPassword.AccountId);
                                    if (item2 != null)
                                    {
                                        item2.Status = 2;
                                        _forgetPasswordRepository.UpdateAsync(item2);
                                        await _forgetPasswordRepository.SaveAsync();
                                    }

                                    message = "Yêu cầu của bạn đã được xử lý không thành công. Vui lòng thử lại!";
                                }
                            }
                            else
                            {
                                message = "Lỗi khi kích hoạt mật khẩu";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                message = "Lỗi khi kích hoạt mật khẩu: " + ex.Message;
            }
            ViewBag.Url     = "https://" + _httpContextAccessor.HttpContext.Request.Host.Value;
            ViewBag.Message = message;
            return(View());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> ForgotPassword(string email)
        {
            string message;
            var    checkRegister     = _accountRepository.All.Count(u => u.Email == email);
            var    checkRegisterNull = _accountRepository.All.FirstOrDefault(u => u.Email == email);

            if (checkRegister != 0 && checkRegisterNull != null)
            {
                string activeCode = StringHelper.CreateRandomString(32);
                activeCode = StringHelper.StringToMd5(activeCode).ToLower();
                var temporaryPassword = StringHelper.CreateRandomString(8);

                ForgetPassword forgetPassword = new ForgetPassword
                {
                    AccountId         = checkRegisterNull.Id,
                    ActiveCode        = activeCode,
                    RequestTime       = DateTime.Now,
                    TemporaryPassword = Base64Hepler.Base64Encode(StringHelper.KillChars(temporaryPassword).ToLower()),
                    Status            = 0,
                    RequestIp         = Request.HttpContext.Connection.RemoteIpAddress.ToString()
                };
                await _forgetPasswordRepository.AddAsync(forgetPassword);

                await _forgetPasswordRepository.SaveAsync();

                var systemInfo = _systemInformationRepository.All.FirstOrDefault();

                if (systemInfo != null)
                {
                    string        domainName = _httpContextAccessor.HttpContext.Request.Host.Value;
                    StringBuilder body       = new StringBuilder();
                    body.Append("Kính gửi " + StringHelper.KillChars(checkRegisterNull.Email) + ",<br /><br />");
                    body.Append("Quí vị đã yêu cầu khôi phục mật khẩu trên website " + systemInfo.SiteName + "!<br />");
                    body.Append("Mật khẩu mới của quí vị là: " + temporaryPassword);
                    body.Append("<br />Quí vị vui lòng bấm ");
                    body.Append("<a href=\"" + "https://" + domainName + "/Auth/xac-nhan-khoi-phuc-mat-khau/" + activeCode + "\" target=\"_blank\">vào đây</a>");
                    body.Append(" để xác thực việc quên mật khẩu. <br />");
                    body.Append(" Yêu cầu của quí vị chỉ có hiệu lực trong 24 giờ. <br />");
                    body.Append("<br /><br />Vô cùng xin lỗi nếu email này làm phiền quí vị!<br /><br />");
                    body.Append("<br />Kính thư, <br /><br />");
                    body.Append(systemInfo.SiteName + "<br />");
                    body.Append("Phát triển bởi Công ty Thiết kế Nội thất<br />");
                    body.Append("Webmaster: [email protected]");


                    bool result2 = await SendEmail.SendAsync(systemInfo.SMTPName, systemInfo.SMTPEmail, systemInfo.SMTPPassword, email, "Xác nhận khôi phục mật khẩu", body.ToString());

                    if (!result2)
                    {//Gửi email không thành công
                        var item = _forgetPasswordRepository.All.FirstOrDefault(x => x.AccountId == forgetPassword.AccountId);
                        if (item != null)
                        {
                            item.Status = 4;
                            _forgetPasswordRepository.UpdateAsync(item);
                        }
                        await _forgetPasswordRepository.SaveAsync();

                        message = "Đã xảy ra lỗi khi thực hiện yêu cầu của bạn! (không gửi được email)";
                    }
                    else
                    {
                        message = "Yêu cầu khôi phục mật khẩu của bạn đã được chấp nhận. Vui lòng kiểm tra email và làm theo hướng dẫn.";
                    }
                }
                else
                {
                    message = "Lỗi không xác định! Vui lòng thử lại!";
                }
            }
            else
            {
                message = "Địa chỉ email không tồn tại!";
            }
            return(Json(message));
        }
Ejemplo n.º 7
0
        public async Task <ActionResult> ConfirmPassword(string code)
        {
            try
            {
                ViewBag.Message = "";
                var forgetPassword = await _repository.GetRepository <ForgetPassword>().ReadAsync(o => o.ActiveCode == code);

                if (forgetPassword == null)
                {
                    ViewBag.Message = "Yêu cầu của bạn không chính xác. Vui lòng kiểm tra lại!";
                    return(View());
                }
                else
                {
                    if (forgetPassword.Status != 0)
                    {
                        ViewBag.Message = "Yêu cầu của bạn không chính xác. Vui lòng kiểm tra lại!";
                        return(View());
                    }
                    DateTime requestTime = forgetPassword.RequestTime;
                    TimeSpan timespan    = DateTime.Now - requestTime;
                    double   hours       = timespan.TotalHours;
                    if (hours > 24)
                    {
                        ViewBag.Message       = "Yêu cầu khôi phục mật khẩu của bạn đã hết thời hạn!";
                        forgetPassword.Status = 3;
                        int result1 = await _repository.GetRepository <ForgetPassword>().UpdateAsync(forgetPassword, forgetPassword.AccountId);

                        return(View());
                    }
                    var account = await _repository.GetRepository <Account>().ReadAsync(forgetPassword.AccountId);

                    account.Password = Base64Hepler.DecodeFrom64(forgetPassword.TemporaryPassword);
                    int result2 = await _repository.GetRepository <Account>().UpdateAsync(account, forgetPassword.AccountId);

                    if (result2 > 0)
                    {
                        forgetPassword.Status     = 1;
                        forgetPassword.ActiveTime = DateTime.Now;
                        int result1 = await _repository.GetRepository <ForgetPassword>().UpdateAsync(forgetPassword, forgetPassword.AccountId);

                        ViewBag.Message = "Yêu cầu của bạn đã được xử lý thành công. Bạn đã có thể dùng mật khẩu mới để truy cập!";
                        //Có cần gửi email thông báo là đổi mật khẩu thành công hay không?
                        return(View());
                    }
                    else
                    {
                        forgetPassword.Status = 2;
                        int result1 = await _repository.GetRepository <ForgetPassword>().UpdateAsync(forgetPassword, forgetPassword.AccountId);

                        ViewBag.Message = "Yêu cầu của bạn đã được xử lý không thành công. Vui lòng thử lại!";
                        return(View());
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error = "Lỗi khi kích hoạt mật khẩu: " + ex.Message;
                return(View());
            }
        }
Ejemplo n.º 8
0
        public async Task <ActionResult> ForgetPassword(ForgetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                string email   = StringHelper.KillChars(model.Email);
                var    account = await _repository.GetRepository <Account>().ReadAsync(o => o.Email == email);

                if (account == null)
                {
                    ModelState.AddModelError(string.Empty, "Địa chỉ email không tồn tại!");
                    return(View(model));
                }
                else
                {
                    string activeCode = StringHelper.CreateRandomString(32);
                    activeCode = StringHelper.StringToMd5(activeCode).ToLower();
                    var temporaryPassword = StringHelper.CreateRandomString(8);

                    ForgetPassword forgetPassword = new Entities.Models.ForgetPassword();
                    forgetPassword.AccountId         = account.Id;
                    forgetPassword.ActiveCode        = activeCode;
                    forgetPassword.RequestTime       = DateTime.Now;
                    forgetPassword.TemporaryPassword = Base64Hepler.EncodeTo64UTF8(StringHelper.StringToMd5(StringHelper.StringToMd5(StringHelper.KillChars(temporaryPassword)).ToLower()));
                    forgetPassword.Status            = 0;
                    forgetPassword.RequestIp         = CommonHelper.GetIPAddress(Request);
                    int result = await _repository.GetRepository <ForgetPassword>().CreateAsync(forgetPassword, account.Id);

                    var systemInfo = (await _repository.GetRepository <SystemInformation>().GetAllAsync()).FirstOrDefault();

                    if (result > 0 && systemInfo != null)
                    {
                        string        domainName = Request.Url.Scheme + "://" + Request.Url.Authority;
                        StringBuilder body       = new StringBuilder();
                        body.Append("Kính gửi " + StringHelper.KillChars(account.Name) + ",<br /><br />");
                        body.Append("Quí vị đã yêu cầu khôi phục mật khẩu trên website " + systemInfo.SiteName + "!<br />");
                        body.Append("Mật khẩu mới của quí vị là: " + temporaryPassword);
                        body.Append("<br />Quí vị vui lòng bấm ");
                        body.Append("<a href=\"" + domainName + "/xac-nhan-khoi-phuc-mat-khau/" + activeCode + "\" target=\"_blank\">vào đây</a>");
                        body.Append(" để xác thực việc quên mật khẩu. <br />");
                        body.Append(" Yêu cầu của quí vị chỉ có hiệu lực trong 24 giờ. <br />");
                        body.Append("<br /><br />Vô cùng xin lỗi nếu email này làm phiền quí vị!<br /><br />");
                        body.Append("<br />Kính thư, <br /><br />");
                        body.Append(systemInfo.SiteName + "<br />");
                        body.Append("Phát triển bởi eBtech Team<br />");
                        body.Append("Webmaster: [email protected]");

                        bool result2 = await SendEmail.SendAsync(systemInfo.SMTPName, systemInfo.SMTPEmail, systemInfo.SMTPPassword, email, "Xác nhận khôi phục mật khẩu", body.ToString());

                        if (!result2)
                        {//Gửi email không thành công
                            forgetPassword.Status = 4;
                            int result1 = await _repository.GetRepository <ForgetPassword>().UpdateAsync(forgetPassword, account.Id);

                            ModelState.AddModelError(string.Empty, "Đã xảy ra lỗi khi thực hiện yêu cầu của bạn! (không gửi được email)");
                            return(View(model));
                        }
                        else
                        {
                            TempData["Success"] = true;
                            return(RedirectToRoute("FrontEndLoginForgetPassword"));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Vui lòng nhập đầy đủ các thông tin!");
                        return(View(model));
                    }
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Vui lòng nhập đầy đủ các thông tin!");
                return(View(model));
            }
        }