Ejemplo n.º 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();
     }
 }
Ejemplo n.º 2
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);
            }
        }