Exemple #1
0
        public ActionResult Register(Account model)
        {
            if (ModelState.IsValid)
            {
                var dao = new AccountDAO();
                if (dao.CheckUserName(model.UserName))
                {
                    ViewBag.Message = "Tên đăng nhập đã tồn tại";
                }
                else if (dao.CheckEmail(model.Email))
                {
                    ViewBag.Message = "Email đã tồn tại";
                }
                else
                {
                    var user = new Account();
                    user.UserName    = model.UserName;
                    user.Pass        = Encryptor.MD5Hash(model.Pass);
                    user.Phone       = model.Phone;
                    user.Address     = model.Address;
                    user.Email       = model.Email;
                    user.Level       = 2;
                    user.CreatedDate = DateTime.Now;
                    user.ExpiryDate  = DateTime.Now;
                    user.Status      = "false";
                    user.LockNote    = "Ban chua kich hoat";
                    user.Rating      = 0;
                    user.NoRating    = 0;
                    var result = dao.Insert(user);
                    if (result > 0)
                    {
                        int    userid       = dao.GetIdUser(model.UserName);
                        string smtpUserName = "******";
                        string smtpPassword = "******";
                        string smtpHost     = "smtp.gmail.com";
                        int    smtpPort     = 25;
                        string emailTo      = model.Email;
                        string subject      = "Xác minh tài khoản của bạn";
                        string path         = Server.MapPath(@"/public/images/facebook.png");
                        //now do the HTML formatting
                        //@Request.Url.GetLeftPart(UriPartial.Authority)
                        string body = string.Format(
                            "<img src=@'/public/images/facebook.png'/><br/>Xin chào {0}.<br/>Cảm ơn bạn đã đăng kí tài khoản tại TATSHOP. Vui lòng nhấn vào đường dẫn bên dưới để hoàn tất đăng kí.<br/><a href=\"{1}\" title=\"Xác nhận tài khoản của bạn\">Xác nhận</a>", model.UserName, Url.Action("ConfirmEmail", "Home", new { Token = userid, Email = model.Email }, Request.Url.Scheme));


                        EmailService service = new EmailService();
                        bool         kq      = service.Send(smtpUserName, smtpPassword, smtpHost, smtpPort,
                                                            emailTo, subject, body, path);
                        model = new Account();
                        return(RedirectToAction("Confirm", "Home", new { Email = user.Email }));
                    }
                    else
                    {
                        ViewBag.Message = "Đăng kí chưa thành công. Vui lòng thử lại.";
                    }
                }
            }
            return(View(model));
        }