예제 #1
0
 public ActionResult Registration(UserDetails userdetails, string returnUrl)
 {
     try
     {
         if (ModelState.IsValid)
         {
             // Checking the username availability in the server
             BTourGuideOp op = new BTourGuideOp();
             List<AUser> users = op.GetUsers();
             if (!users.Any(u => u.Username == userdetails.Username))
             {
                 // password salting & hashing
                 PasswordManager passMan = new PasswordManager();
                 string salt = null;
                 string passwordHash = passMan.GeneratePasswordHash(userdetails.UserPassword, out salt);
                 AUser user = new AUser();
                 user.RegTime = DateTime.Now;
                 user.UserIP = Request.ServerVariables["REMOTE_ADDR"];
                 user.UserFirstName = userdetails.UserFirstName;
                 user.UserLastName = userdetails.UserLastName;
                 user.UserEmail = userdetails.UserEmail;
                 user.UserPhone = userdetails.UserPhone;
                 user.UserPassword = passwordHash;
                 user.Salt = salt;
                 user.Username = userdetails.Username;
                 user.UserBirthday = userdetails.UserBirthday;
                 BTourGuideOp tourOp = new BTourGuideOp();
                 tourOp.AddUser(user);
                 return RedirectToAction("Login", "Account");
             }
             else
             {
                 userdetails.Username = null;
                 return View();
             }
         }
         else
         {
             userdetails.Username = null;
             return View();
         }
     }
     catch(Exception e)
     {
         TempData["Exception"] = "" + e.Message;
         return View();
     }
 }
예제 #2
0
 public ActionResult Edit(string id, UserDetails userDetails)
 {
     try
     {
         if (ModelState.IsValid)
         {
             BTourGuideOp tourOp = new BTourGuideOp();
             AUser user = tourOp.GetUser(userDetails.Username);
             user.UserFirstName = userDetails.UserFirstName;
             user.UserLastName = userDetails.UserLastName;
             user.UserPhone = userDetails.UserPhone;
             user.UserEmail = userDetails.UserEmail;
             user.UserBirthday = userDetails.UserBirthday;
             tourOp.EditUser(user);
             return RedirectToAction("Index");
         }
         else
             return View(userDetails);
     }
     catch(Exception e)
     {
         TempData["EditException"] = "Error in user edit: " + e.Message;
         return View(userDetails);
     }
 }
예제 #3
0
        public ActionResult Create(UserDetails userdetails)
        {
            try
            {
                if (ModelState.IsValid)
                {
                     // Checking the username availability in the server
                      BTourGuideOp op = new BTourGuideOp();
                      List<AUser> users = op.GetUsers();
                      if (!users.Any(u => u.Username == userdetails.Username))
                      {
                          BTourGuideOp tourOp = new BTourGuideOp();
                          AUser user = new AUser();
                          user.RegTime = DateTime.Now;
                          user.UserIP = Request.ServerVariables["REMOTE_ADDR"];
                          user.UserFirstName = userdetails.UserFirstName;
                          user.UserLastName = userdetails.UserLastName;
                          user.UserEmail = userdetails.UserEmail;
                          user.UserPhone = userdetails.UserPhone;

                          // Create a random password
                          string password = System.Web.Security.Membership.GeneratePassword(8, 2);
                          // hash and salt the password
                          PasswordManager passMan = new PasswordManager();
                          string salt = null;
                          string hashPassword = passMan.GeneratePasswordHash(password, out salt);

                          user.UserPassword = hashPassword;
                          user.Salt = salt;
                          user.Username = userdetails.Username;
                          user.UserBirthday = userdetails.UserBirthday;
                          tourOp.AddUser(user);

                          // Generae password token that will be used in the email link to authenticate user
                          string resetToken = Guid.NewGuid().ToString();

                          // Hash the reset token
                          HashComputer hashComp = new HashComputer();
                          string resetTokenHash = hashComp.GetPasswordHashAndSalt(resetToken);

                          AUser theNewUser = tourOp.GetUser(user.Username);

                          // Generate the html link sent via email
                          theNewUser.ResetToken = resetTokenHash;
                          tourOp.EditUser(theNewUser);

                          // Email stuff
                          string subject = "New account in TourGuideWebsite";
                          string body = "You have a new account in TourGuideWebsite. " +
                                         "To reset your password <a href='" + Url.Action("ResetPassword", "Account", new { rt = resetToken }, "http")
                                         + "'>Click here</a>";

                          string from = "*****@*****.**";

                          MailMessage message = new MailMessage(from, user.UserEmail);
                          message.Subject = subject;
                          message.Body = body;
                          message.IsBodyHtml = true;

                          SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                          {
                              UseDefaultCredentials = false,
                              EnableSsl = true,
                              Timeout = 20000,
                              Credentials = new NetworkCredential("*****@*****.**", "henhqwcfvmtzplgb")

                          };

                          // Attempt to send the email
                          try
                          {
                              client.Send(message);
                          }
                          catch (Exception e)
                          {
                             TempData["EmailException"] = "Issue sending email: " + e.Message;
                          }
                          return RedirectToAction("Index");
                      }
                      else
                      {
                          userdetails.Username = null;
                          return View();
                      }
                }
                else
                {
                    return View(userdetails);
                }
            }
            catch(Exception e)
            {
                TempData["Exception"] = "" + e.Message;
                return View(userdetails);
            }
        }
예제 #4
0
        //
        // GET: /User/Edit/5
        public ActionResult Edit(string id, DateTime UserBirthday)
        {
            BTourGuideOp tourOp = new BTourGuideOp();
            List<AUser> users = tourOp.GetUsers();
            AUser user = users.Single<AUser>(x => x.UserID == id);
            UserDetails userDetails = new UserDetails();
            userDetails.UserBirthday = user.UserBirthday;
            userDetails.UserEmail = user.UserEmail;
            userDetails.UserPhone = user.UserPhone;
            userDetails.UserFirstName = user.UserFirstName;
            userDetails.UserLastName = user.UserLastName;
            userDetails.Username = user.Username;

            // Not the true password
            userDetails.UserPassword = "******";
            userDetails.ConfirmPass = "******";
            return View(userDetails);
        }