public ActionResult ForgotPassword(ForgotPasswordModel entity)
 {
     if (ModelState.IsValid)
     {
         var    dao     = new UserDao();
         string newPass = GeneratePassword.Generate();
         int    check   = dao.changePassword(entity.UserName, entity.Email, newPass);
         if (check == 1)
         {
             string content = "Mật khẩu mới của bạn là: <b>" + newPass + "</b>";
             new MailHelper().SendEmail(entity.Email, "Mobile World - Quên mật khẩu", content);
             //ModelState.AddModelError("", "Mật khẩu mới đã được gửi vào email của bạn");
             string message = "Mật khẩu mới đã được gửi vào email của bạn";
             TempData["Message"] = message;
             return(RedirectToAction("index", "login"));
         }
         else if (check == -2)
         {
             ModelState.AddModelError("", "Email không đúng");
         }
         else
         {
             ModelState.AddModelError("", "Tài khoản không đúng");
         }
     }
     return(View("ForgotPassword"));
 }
Пример #2
0
        public Dictionary <int, string> AuthAccount(UserDTO userDTO)
        {
            Dictionary <int, string> result = new Dictionary <int, string>();
            var user = _context.Users.SingleOrDefault(x => x.username == userDTO.username);

            if (user == null)
            {
                result.Add(0, "");
            }
            else if (!user.email.Equals(userDTO.email))
            {
                result.Add(-1, "");
            }
            else
            {
                var password = GeneratePassword.Generate();
                user.password = Hashing.HashPassword(password);
                try
                {
                    _context.SaveChanges();
                }
                catch (Exception)
                {
                    result.Add(-2, "");
                }
                result.Add(1, password);
            }
            return(result);
        }
Пример #3
0
        protected void RadButtonSend_Click(object sender, EventArgs e)
        {
            string         userName       = TextBoxUserName.Text;
            MembershipUser membershipUser = Membership.GetUser(userName);

            if (membershipUser != null)
            {
                String password = GeneratePassword.Generate(10, 10);
                membershipUser.ChangePassword(membershipUser.ResetPassword(), password);

                String emailTemplate = HttpContext.Current.Server.MapPath(Path.Combine(@"~\Files\EmailTemplates", "MailTemplateChangePasswordSuccesfull_eng.html"));

                StreamReader streamReader    = new StreamReader(emailTemplate);
                String       mailMessageBody = streamReader.ReadToEnd();

                String fromEmailAddress;
                String subject;
                String toEmailAddress;

                mailMessageBody = EmailClass.GetMailParams(mailMessageBody, out fromEmailAddress, out toEmailAddress, out subject);

                mailMessageBody = mailMessageBody.Replace("{Username}", userName);
                mailMessageBody = mailMessageBody.Replace("{Password}", password);

                try
                {
                    MailMessage mailMessage = new MailMessage();
                    mailMessage.IsBodyHtml = true;
                    mailMessage.From       = new MailAddress(fromEmailAddress);
                    mailMessage.To.Add(userName);
                    mailMessage.Subject = subject;
                    mailMessage.Body    = mailMessageBody;
                    SmtpClient smtpClient = new SmtpClient();
                    smtpClient.Send(mailMessage);

                    PanelForgetPassword.Visible = false;
                    LiteralPasswordRequestSuccesfull.Visible = true;
                }
                catch (Exception ex)
                {
                    // TODO: Create errorpage for user
                    Response.Redirect("~/Pages/OrderSuccess.aspx");
                }
            }
            else
            {
                LiteralUsernameNotFound.Visible = true;
            }
        }
        public ActionResult Index([Bind(Exclude = "SCHOOL_ID")] ApplicationModel model)
        {
            //Thread.Sleep(5000);
            if (!ModelState.IsValid)
            {
                /*
                 * TITLE PROVINCE
                 */
                IList <TB_M_PROVINCE>        mProvinceLists = db.TB_M_PROVINCE.ToList <TB_M_PROVINCE>();
                IEnumerable <SelectListItem> provinceLists  =
                    from s in mProvinceLists
                    select new SelectListItem
                {
                    Text  = s.PROVINCE_NAME,
                    Value = s.PROVINCE_ID.ToString()
                };



                /*
                 * SCHOOL TYPE
                 */
                List <RadioButtonModel> list = new List <RadioButtonModel>();

                list.Add(new RadioButtonModel()
                {
                    ID = 1, Name = Resources.Application.Application.SCHOOL_TYPE_01
                });                                                                                                  //สพฐ
                list.Add(new RadioButtonModel()
                {
                    ID = 2, Name = Resources.Application.Application.SCHOOL_TYPE_02
                });                                                                                                  //เอกชน
                list.Add(new RadioButtonModel()
                {
                    ID = 3, Name = Resources.Application.Application.SCHOOL_TYPE_03
                });                                                                                                  //กทม
                list.Add(new RadioButtonModel()
                {
                    ID = 4, Name = Resources.Application.Application.SCHOOL_TYPE_04
                });                                                                                                  //อุดมศึกษา
                list.Add(new RadioButtonModel()
                {
                    ID = 5, Name = Resources.Application.Application.SCHOOL_TYPE_OTHER
                });                                                                                                     //อื่น ๆ

                SelectList schoolTypes = new SelectList(list, "ID", "Name");
                model.provinceLists = provinceLists;
                model.rSchoolTypes  = schoolTypes;
                return(View(model));
            }

            //using (ChinaPhet10Entities context = new ChinaPhet10Entities())
            //{

            //using (TransactionScope tran = new TransactionScope())
            //{
            try
            {
                //Check school duplicate
                var isExist = db.TB_APPLICATION_SCHOOL.Any(k => k.SCHOOL_EMAIL == model.school.SCHOOL_EMAIL);
                if (!isExist)
                {
                    /*
                     * SCHOOL MODEL
                     */
                    CultureInfo    ci          = (CultureInfo)this.Session["PhetCulture"];
                    TB_APPLICATION application = db.TB_APPLICATION.FirstOrDefault();


                    //VALIDATE NULL VALUE
                    model.school.SCHOOL_ADDR_PROVINCE = (model.SCHOOL_ADDR_PROVINCE == null) ? -1 : Convert.ToInt32(model.SCHOOL_ADDR_PROVINCE);
                    model.school.SCHOOL_ADDR_AMPHUR   = (model.SCHOOL_ADDR_AMPHUR == null) ? -1 : Convert.ToInt32(model.SCHOOL_ADDR_AMPHUR);
                    model.school.SCHOOL_ADDR_TOMBON   = (model.SCHOOL_ADDR_TOMBON == null) ? -1 : Convert.ToInt32(model.SCHOOL_ADDR_TOMBON);
                    model.school.SCHOOL_ZONE_EDU      = (model.school.SCHOOL_ZONE_EDU == null) ? "" : model.school.SCHOOL_ZONE_EDU;
                    model.school.SCHOOL_ZONE          = (model.school.SCHOOL_ZONE == null) ? "" : model.school.SCHOOL_ZONE;
                    model.school.SCHOOL_TYPE_OTHER    = (model.school.SCHOOL_TYPE_OTHER == null) ? "" : model.school.SCHOOL_TYPE_OTHER;
                    model.school.SCHOOL_ADDR          = (model.school.SCHOOL_ADDR == null) ? "" : model.school.SCHOOL_ADDR;
                    model.school.SCHOOL_ADDR_SOI      = (model.school.SCHOOL_ADDR_SOI == null) ? "" : model.school.SCHOOL_ADDR_SOI;
                    model.school.SCHOOL_ADDR_ROAD     = (model.school.SCHOOL_ADDR_ROAD == null) ? "" : model.school.SCHOOL_ADDR_ROAD;
                    model.school.SCHOOL_ADDR_ZIPCODE  = (model.school.SCHOOL_ADDR_ZIPCODE == null) ? "" : model.school.SCHOOL_ADDR_ZIPCODE;
                    model.school.SCHOOL_ADDR_PHONE    = (model.school.SCHOOL_ADDR_PHONE == null) ? "" : model.school.SCHOOL_ADDR_PHONE;
                    model.school.SCHOOL_ADDR_FAX      = (model.school.SCHOOL_ADDR_FAX == null) ? "" : model.school.SCHOOL_ADDR_FAX;
                    model.school.SCHOOL_NAME          = (model.school.SCHOOL_NAME == null) ? "" : model.school.SCHOOL_NAME;
                    model.school.SCHOOL_PROVINCE      = (model.school.SCHOOL_PROVINCE == null) ? -1 : model.school.SCHOOL_PROVINCE;
                    model.school.SCHOOL_PASSWORD      = (model.school.SCHOOL_PASSWORD == null) ? "" : model.school.SCHOOL_PASSWORD;
                    model.school.SCHOOL_DOC_PATH      = (model.school.SCHOOL_DOC_PATH == null) ? "" : model.school.SCHOOL_DOC_PATH;
                    model.school.SCHOOL_REGISTER_DATE = (model.school.SCHOOL_REGISTER_DATE == null) ? DateTime.Now : model.school.SCHOOL_REGISTER_DATE;

                    model.school.SCHOOL_EMAIL   = (model.school.SCHOOL_EMAIL == null) ? "" : model.school.SCHOOL_EMAIL;
                    model.school.SCHOOL_CULTURE = ci.Name;
                    model.school.SCHOOL_ROUND   = Convert.ToInt32(application.PROJECT_ROUND);



                    model.school.SCHOOL_TYPE            = (model.idSelectedSchoolType == null) ? "" : model.idSelectedSchoolType;
                    model.school.SCHOOL_APPROVED_STATUS = 1;        //Y=Approved,N=DisApproved

                    /* generate password*/
                    String generatedPassword = GeneratePassword.Generate(8);
                    model.school.SCHOOL_PASSWORD = generatedPassword;        // MD5.md5(generatedPassword);
                    db.TB_APPLICATION_SCHOOL.Add(model.school);
                    db.SaveChanges();

                    /*
                     * STAFF
                     */
                    if (model.Staffs != null)
                    {
                        foreach (TB_APPLICATION_STAFF staff in model.Staffs)
                        {
                            ////VALIDATE NULL VALUE
                            staff.STAFF_SCHOOL_ID = model.school.SCHOOL_ID;
                            staff.STAFF_POSITION  = (staff.STAFF_POSITION == null) ? 0 : staff.STAFF_POSITION;
                            staff.STAFF_NAME      = (staff.STAFF_NAME == null) ? "" : staff.STAFF_NAME;
                            staff.STAFF_SURNAME   = (staff.STAFF_SURNAME == null) ? "" : staff.STAFF_SURNAME;
                            staff.STAFF_PHONE     = (staff.STAFF_PHONE == null) ? "" : staff.STAFF_PHONE;
                            staff.STAFF_TITLE_ID  = (staff.STAFF_TITLE_ID == null) ? 0 : staff.STAFF_TITLE_ID;
                            staff.STAFF_FOR_LEVEL = (staff.STAFF_FOR_LEVEL == null) ? 0 : staff.STAFF_FOR_LEVEL;

                            if (!String.IsNullOrEmpty(staff.STAFF_NAME) && !String.IsNullOrEmpty(staff.STAFF_SURNAME))
                            {
                                staff.STAFF_SCHOOL_ID = model.school.SCHOOL_ID;
                                db.TB_APPLICATION_STAFF.Add(staff);
                            }
                        }
                    }

                    /*
                     * STUDENT
                     */
                    if (model.Students != null)
                    {
                        foreach (TB_APPLICATION_STUDENT student in model.Students)
                        {
                            //VALIDATE NULL VALUE
                            student.STD_TITLE_ID        = (student.STD_TITLE_ID == null) ? 0 : student.STD_TITLE_ID;
                            student.STD_NAME            = (student.STD_NAME == null) ? "" : student.STD_NAME;
                            student.STD_SURNAME         = (student.STD_SURNAME == null) ? "" : student.STD_SURNAME;
                            student.STD_LEVEL_ID        = (student.STD_LEVEL_ID == null) ? 0 : student.STD_LEVEL_ID;
                            student.STD_SCHOOL_ID       = model.school.SCHOOL_ID;
                            student.STD_PICTURE_PATH    = (student.STD_PICTURE_PATH == null) ? "" : student.STD_PICTURE_PATH;
                            student.STD_PHONE           = (student.STD_PHONE == null) ? "" : student.STD_PHONE;
                            student.STD_EMAIL           = (student.STD_EMAIL == null) ? "" : student.STD_EMAIL;
                            student.STD_ID_CARD         = (student.STD_ID_CARD == null) ? "" : student.STD_ID_CARD;
                            student.STD_PASSPORT_ID     = (student.STD_PASSPORT_ID == null) ? "" : student.STD_PASSPORT_ID;
                            student.STD_GRADE           = (student.STD_GRADE == null) ? 0 : student.STD_GRADE;
                            student.STD_PHONE_PROVIDER  = (student.STD_PHONE_PROVIDER == null) ? "" : student.STD_PHONE_PROVIDER;
                            student.STD_APPROVED_STATUS = 1;
                            student.STD_IS_CONCERN      = "0";
                            student.STD_NATION          = student.STD_NATION;
                            student.STD_NATION_OTHER    = student.STD_NATION_OTHER;

                            #region "CONCERN STUDENT."
                            bool isValidLevel = false;
                            int  currentYear  = DateTime.Now.Year;
                            if (!String.IsNullOrEmpty(student.STD_BIRTH_DAY))
                            {
                                currentYear = currentYear - Convert.ToInt16(student.STD_BIRTH_DAY.Split('-')[0]);
                            }

                            if (currentYear <= 9)
                            {
                                if (student.STD_LEVEL_ID == 1)
                                {
                                    isValidLevel = true;;
                                }
                            }
                            else if (currentYear >= 10 && currentYear <= 12)
                            {
                                if (student.STD_LEVEL_ID == 2)
                                {
                                    isValidLevel = true;
                                }
                            }
                            else if (currentYear >= 13 && currentYear <= 15)
                            {
                                if (student.STD_LEVEL_ID == 3)
                                {
                                    isValidLevel = true;
                                }
                            }
                            else if (currentYear >= 16 && currentYear <= 18)
                            {
                                if (student.STD_LEVEL_ID == 4)
                                {
                                    isValidLevel = true;
                                }
                            }
                            else
                            {
                                if (student.STD_LEVEL_ID == 5)
                                {
                                    isValidLevel = true;
                                }
                            }
                            if (isValidLevel == false)
                            {
                                student.STD_IS_CONCERN = "1";
                            }
                            #endregion

                            if (!String.IsNullOrEmpty(student.STD_NAME) && !String.IsNullOrEmpty(student.STD_SURNAME))
                            {
                                db.TB_APPLICATION_STUDENT.Add(student);
                            }
                        }
                    }

                    db.SaveChanges();
                    //tran.Complete();

                    /* send email school account to user*/
                    if (!String.IsNullOrEmpty(model.school.SCHOOL_EMAIL))
                    {
                        if (Email.IsValidEmail(model.school.SCHOOL_EMAIL))
                        {
                            Email.SendEmail(model.school, generatedPassword, ci.Name);
                        }
                        else
                        {
                            logger.Debug(model.school.SCHOOL_ID + "," + model.school.SCHOOL_NAME + "invalid email format.");
                        }
                    }
                    else
                    {
                        logger.Debug(model.school.SCHOOL_ID + "," + model.school.SCHOOL_NAME + "no have email.");
                    }
                }
                else
                {
                    return(View("Exception"));
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        errorMessage += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
                    }
                }
                throw new Exception(errorMessage, dbEx);
            }
            //}
            //}
            ViewBag.UserName    = model.school.SCHOOL_EMAIL;
            ViewBag.Password    = model.school.SCHOOL_PASSWORD;
            ViewBag.PageContent = Resources.Application.Application.REGISTER_ITEM016;
            return(View("Complete"));
        }
Пример #5
0
        public void CreateUsers(businessPartner customer, String companyCode, DALPortalDataContext dc)
        {
            foreach (var contactPerson in customer.contactPersons.Where(c => c.businessPartnerId.Equals(customer.businessPartnerId)))
            {
                try
                {
                    if (contactPerson.eMail == null)
                    {
                        continue;
                    }

                    MembershipUser user = Membership.GetUser(contactPerson.eMail);

                    Boolean newUser = (user == null);
                    Guid    newUserId;
                    String  password = String.Empty;

                    if (newUser && contactPerson.isWebContact)
                    {
                        object akey = Guid.NewGuid();
                        password = GeneratePassword.Generate(10, 10);

                        Membership.CreateUser(contactPerson.eMail, password, contactPerson.eMail);
                        Roles.AddUserToRole(contactPerson.eMail, "User");
                    }

                    if (contactPerson.userId == null && contactPerson.isWebContact)
                    {
                        newUserId = dc.Users.Where(c => c.UserName.ToLower().Equals(contactPerson.eMail.ToLower())).Select(c => c.UserId).FirstOrDefault();

                        contactPerson.userId = newUserId;

                        dc.contactPersons.Where(c => c.contactPersonCode.Equals(contactPerson.contactPersonCode)).FirstOrDefault().userId = newUserId;
                        dc.SubmitChanges();
                    }

                    // Contact person in database, but not in xml file
                    if (contactPerson.TMP_PortalAccess == null)
                    {
                        dc.contactPersons.Where(c => c.contactPersonCode.Equals(contactPerson.contactPersonCode)).FirstOrDefault().isWebContact = false;
                        dc.SubmitChanges();
                    }

                    // Deactivate the user account
                    // Existing user which is not a webcontact anymore or when user is not in file anymore.
                    if ((!newUser && !contactPerson.isWebContact) || contactPerson.TMP_PortalAccess == null)
                    {
                        if (user != null)
                        {
                            if (user.IsApproved == true)
                            {
                                user.IsApproved = false;
                                Membership.UpdateUser(user);
                            }
                        }
                    }

                    // Activate the user account
                    if (!newUser && contactPerson.isWebContact && contactPerson.TMP_PortalAccess != null)
                    {
                        if (user.IsApproved == false)
                        {
                            user.IsApproved = true;
                            Membership.UpdateUser(user);
                        }
                    }

                    if (contactPerson.userId.HasValue)
                    {
                        UpdateApplicationRoles(contactPerson, dc);
                    }

                    if (newUser && contactPerson.isWebContact)
                    {
                        // Send Email
                        SendMail(contactPerson.eMail, password, companyCode);
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("An error occurred while creating user: "******". Error: " + ex.Message, "CreateUsers");
                }
            }
        }