Ejemplo n.º 1
0
        public bool AddUser(AUser userReg)
        {
            using (DataClassesTourGuideDataContext dc = new DataClassesTourGuideDataContext())
            {
                User user = new User();
                user.RegTime = (DateTime)userReg.RegTime;
                user.RegIP = userReg.UserIP;
                user.UserFirstName = userReg.UserFirstName;
                user.UserLastName = userReg.UserLastName;
                user.UserPhone = userReg.UserPhone;
                user.UserEmail = userReg.UserEmail;
                user.UserPassword = userReg.UserPassword;
                user.UserBirthday = userReg.UserBirthday;
                user.Username = userReg.Username;
                user.salt = userReg.Salt;
                user.UserID = System.Guid.NewGuid();

                dc.Users.InsertOnSubmit(user);
                dc.SubmitChanges();
                return true;
            }
        }
Ejemplo n.º 2
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.º 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);
            }
        }
Ejemplo n.º 4
0
 public ActionResult Delete(string id, AUser user)
 {
     try
     {
         BTourGuideOp tourOp = new BTourGuideOp();
         tourOp.DeleteUser(id);
         return RedirectToAction("Index");
     }
     catch(Exception e)
     {
         TempData["DeleteException"] = "Error in user deletion: " + e.Message;
         return View();
     }
 }
Ejemplo n.º 5
0
        public bool EditUser(AUser user)
        {
            using (DataClassesTourGuideDataContext dc = new DataClassesTourGuideDataContext())
            {

                User row = (from c in dc.Users
                            where (c.UserID.ToString() == user.UserID)
                            select c).FirstOrDefault<User>();
                row.UserBirthday = user.UserBirthday;
                row.UserEmail = user.UserEmail;
                row.UserFirstName = user.UserFirstName;
                row.UserLastName = user.UserLastName;
                row.Username = user.Username;
                row.UserPassword = user.UserPassword;
                row.UserPhone = user.UserPhone;
                row.ResetToken = user.ResetToken;
                row.salt = user.Salt;
                dc.SubmitChanges();
                return true;
            }
        }