public bool CheckUsernameAvaiability(Username username) { BTourGuideOp op = new BTourGuideOp(); List<AUser> users = op.GetUsers(); if(users.Any(u=>u.Username==username.Name)) { return false; } else return true; }
public static List<SelectListItem> CreateUserList() { // Creating a dropdown list for Users Names: List<SelectListItem> userNames = new List<SelectListItem>(); BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); foreach (AUser user in users) { userNames.Add(new SelectListItem { Text = user.Username, Value = user.Username }); } return userNames; }
public ActionResult Create(AReg reg, string TourNameOptions, string UsernameOptions, string TourDateOptions) { try { if (ModelState.IsValid) { BTourGuideOp tourOp = new BTourGuideOp(); List<ATour> tours = tourOp.GetTours(); ATour tour = tours.Single(x => x.TourName == TourNameOptions); reg.TourID = tour.TourID; List<AUser> users = tourOp.GetUsers(); AUser user = users.Single(x => x.Username == UsernameOptions); reg.UserID = user.UserID; List<AEvent> events = tourOp.GetEvents(); AEvent tourEvent = events.Single(x => x.TourName == TourNameOptions && x.TourDate.ToString() == TourDateOptions.ToString()); reg.TourDate = tourEvent.TourDate; tourOp.AddReg(reg); return RedirectToAction("Index"); } else { // Saving the dropdown values selected by user: List<SelectListItem> tourList = Lists.CreateTourList(); ViewBag.TourNameOptions = tourList; // The initial TourDateOptions ddl fits the first tour already selected by user: string tourName = TourNameOptions; ViewBag.TourDateOptions = Lists.CreateTourDateList(tourName); //The tourDates dropdown list options change based on the tourName choice. This is done with AJAX via query using // a web service. ViewBag.UsernameOptions = Lists.CreateUserList(); return View(reg); } } catch(Exception e) { TempData["CreateException"] = "Error in Reg creation: " + e.Message; return View(reg); } }
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); } }
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(); } }
// // 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); }
// // GET: /User/ public ActionResult Index() { BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); return View(users); }
// // GET: /User/Details/5 public ActionResult Details(string id) { BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); AUser user = users.Single<AUser>(x => x.UserID == id); return View(user); }
// // GET: /User/Delete/5 public ActionResult Delete(string id) { BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); AUser user = users.SingleOrDefault<AUser>(x => x.UserID == id); if (user == null) { return HttpNotFound(); } return View(user); }
public ActionResult ForgotPassword(ForgotPassword model) { try { if (ModelState.IsValid) { // Get the user by email: BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); AUser user = users.FirstOrDefault(u => u.UserEmail == model.Email); if (user != null) // If a user with the email provided was found { // 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); // Generate the html link sent via email user.ResetToken = resetTokenHash; tourOp.EditUser(user); string resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { rt = resetToken }, "http") + "'>Reset Password Link</a>"; // Email stuff string subject = "Reset your password for TourGuideWebsite"; string body = "Your link: " + resetLink; string from = "*****@*****.**"; MailMessage message = new MailMessage(from, model.Email); 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); ViewBag.Message = "A reset password email has been sent."; return View(); } catch (Exception e) { TempData["EmailException"] = "Issue sending email: " + e.Message; } } // For testing: //else // Email not found //{ // /* Note: You may not want to provide the following information // * since it gives an intruder information as to whether a // * certain email address is registered with this website or not. // * If you're really concerned about privacy, you may want to // * forward to the same "Success" page regardless whether an // * user was found or not. This is only for illustration purposes. // */ // ModelState.AddModelError("", "No user found by that email."); //} } return View(model); } catch (Exception e) { TempData["Exception"] = "" + e.Message; return View(model); } }
public ActionResult ResetPassword(ResetPassword model) { try { if (ModelState.IsValid) { BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); // hasing the resetToken from the url HashComputer hashComp = new HashComputer(); string hashedResetToken = hashComp.GetPasswordHashAndSalt(model.ReturnToken); // Checking if the hash matches the resetToken from the DB AUser user = users.FirstOrDefault(u => u.ResetToken == hashedResetToken); if (user != null) { // password salting & hashing PasswordManager passMan = new PasswordManager(); string salt = null; string passwordHash = passMan.GeneratePasswordHash(model.Password, out salt); user.UserPassword = passwordHash; user.Salt = salt; user.ResetToken = null; tourOp.EditUser(user); ViewBag.Message = "Successfully Changed"; } else { ViewBag.Message = "Something went wrong!"; } } return View(model); } catch(Exception e) { TempData["Exception"] = "" + e.Message; return View(); } }
public ActionResult Login(LoginViewModel model, string returnUrl) { try { if (ModelState.IsValid) { BTourGuideOp tourOp = new BTourGuideOp(); List<AUser> users = tourOp.GetUsers(); AUser user = tourOp.GetUser(model.UserName); if (user != null) { // hasing & salting PasswordManager passMan = new PasswordManager(); bool result = passMan.IsPasswordMatch(model.Password, user.Salt, user.UserPassword); if (result) { FormsAuthentication.SetAuthCookie(model.UserName, false); return Redirect(returnUrl ?? Url.Action("Index", "Home")); } else { ModelState.AddModelError("", "Incorrect Username Or Password"); ViewBag.IncorrectInput = "Incorrect"; ViewBag.ReturnUrl = returnUrl; return View(); } } else ModelState.AddModelError("", "Incorrect Username Or Password"); ViewBag.IncorrectInput = "Incorrect"; ViewBag.ReturnUrl = returnUrl; return View(); } return View(); } catch (Exception e) { TempData["LoginException"] = "Login Error: " + e.Message; return View(); } }