Esempio n. 1
0
 // thêm tài khoản mới vào hệ thống
 public bool AddAccount(RegisterViewModel model)
 {
     model.Password = Hash(model.Password);
     string result = dal.AddAccount(model);
     if (result != null)
     {
         SendActivationEmail(model, result);
         return true;
     }
     return false;
 }
        public string AddAccount(RegisterViewModel model)
        {
            User user = new User();
            user.UserName = model.UserName;
            user.PassWord = model.Password;
            user.Email = model.Email;
            user.Admin = false;
            user.FirstName = model.FirstName;
            user.LastName = model.LastName;
            user.Phone = model.Phone;

            user.Products = null;

            string activation_code = Guid.NewGuid().ToString();
            user.ActiveCode = activation_code;

            try
            {
                db.Users.Add(user);
                db.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                return null;
            }
            return activation_code;
        }
 public ActionResult Register(RegisterViewModel model)
 {
     if (ModelState.IsValid)
     {
         if (bus.CheckExistenceAccount(model.Email, model.UserName))
         {
             if (bus.AddAccount(model))
             {
                 ViewBag.Message = "Tài khoản đã được ghi nhận trong hệ thống. Xin vui lòng kiểm tra email để kích hoạt tài khoản.";
                 return View("Result");
             }
             else
             {
                 ViewBag.Alert = "Chúng tôi không thể gửi email kích hoạt đến email của bạn. Xin lỗi vì sự bất tiện này.";
                 return View("Result");
             }
         }
         else
         {
             ViewBag.Message = "Tài khoản đã tồn tại trong hệ thống.";
             return View("Register");
         }
     }
     return View(model);
 }
Esempio n. 4
0
 // send mail active account
 private void SendActivationEmail(RegisterViewModel model, string Active_code)
 {
     using (MailMessage mm = new MailMessage("*****@*****.**", model.Email))
     {
         mm.Subject = "Email xác nhận tài khoản trên website Trao đổi đồ cũ";
         string body = "Thân chào, " + model.UserName + ",";
         body += "<br /><br />Cảm ơn bạn đã đăng ký tài khoản trên website Trao đổi đồ cũ, xin hãy click vào đường link bên dưới để kích hoạt tài khoản.";
         body += "<br /><a href = 'http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + "/Account/Activate?Username="******"&ActivationCode=" + Active_code + "'>Click vào đây để kích hoạt tài khoản.</a>";
         body += "<br /><br />Thanks";
         mm.Body = body;
         mm.IsBodyHtml = true;
         SmtpClient smtp = new SmtpClient();
         smtp.Host = "smtp.gmail.com";
         smtp.EnableSsl = true;
         NetworkCredential NetworkCred = new NetworkCredential("*****@*****.**", "khoahoangtuankiet");
         smtp.UseDefaultCredentials = true;
         smtp.Credentials = NetworkCred;
         smtp.Port = 587;
         smtp.Send(mm);
     }
 }