public ActionResult ResetPassword(ResetPasswordModel model) { var message = ""; if (ModelState.IsValid) { using (BookAppDbEntities dc = new BookAppDbEntities()) { var user = dc.Users.Where(a => a.ResetPasswordCode == model.ResetCode).FirstOrDefault(); if (user != null) { user.Password = Crypto.Hash(model.NewPassword); user.ResetPasswordCode = ""; dc.Configuration.ValidateOnSaveEnabled = false; dc.SaveChanges(); message = "New password updated successfully"; } } } else { message = "Something invalid"; } ViewBag.Message = message; return(View(model)); }
public ActionResult ForgotPassword(string email) { string message = ""; bool status = false; if (string.IsNullOrEmpty(email)) { message = "Error: Email is a required field!"; } else { using (BookAppDbEntities db = new BookAppDbEntities()) { var account = db.Users.Where(a => a.Email == email).FirstOrDefault(); if (account != null) { string resetCode = Guid.NewGuid().ToString(); SendVerificationLinkEmail(account.Email, resetCode, "ResetPassword"); account.ResetPasswordCode = resetCode; db.Configuration.ValidateOnSaveEnabled = false; db.SaveChanges(); message = "Reset password link has been sent to your email id."; } else { message = "Account not found"; } } } ViewBag.Message = message; return(View()); }
public ActionResult Create(Book book) { string fileName = Path.GetFileNameWithoutExtension(book.ImageFile.FileName); string extension = Path.GetExtension(book.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; book.Image = "~/Image/" + fileName; fileName = Path.Combine(Server.MapPath("~/Image/"), fileName); book.ImageFile.SaveAs(fileName); using (BookAppDbEntities bookDb = new BookAppDbEntities()) { if (ModelState.IsValid) { db.Books.Add(book); TempData["successMessage"] = $"Book successfully added"; db.SaveChanges(); return(RedirectToAction("Index")); } else { ModelState.AddModelError(" ", "Error: Please fill in all the fields!"); } } ModelState.Clear(); ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Category1", book.CategoryId); ViewBag.ConditionId = new SelectList(db.Conditions, "Id", "Condition1", book.ConditionId); ViewBag.InstitutionId = new SelectList(db.Institutions, "Id", "Name", book.InstitutionId); return(View(book)); }
public bool DoesEmailExist(string email) { using (BookAppDbEntities dc = new BookAppDbEntities()) { var getMail = dc.Users.Where(a => a.Email == email).FirstOrDefault(); return(getMail != null); } }
public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user) { bool Status = false; string message = ""; ViewBag.GenderId = new SelectList(db.Genders, "Id", "Gender1", user.GenderId); ViewBag.InstitutionId = new SelectList(db.Institutions, "Id", "Name", user.InstitutionId); if (ModelState.IsValid) { #region //Email is already Exist var isExist = DoesEmailExist(user.Email); if (isExist) { ModelState.AddModelError("EmailExist", "Email already exist"); return(View()); } #endregion #region Generate Activation Code user.ActivationCode = Guid.NewGuid(); #endregion #region Password Hashing user.Password = Crypto.Hash(user.Password); user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword); // #endregion user.IsEmailVerified = false; #region Save to Database using (BookAppDbEntities db = new BookAppDbEntities()) { db.Users.Add(user); db.SaveChanges(); SendVerificationLinkEmail(user.Email, user.ActivationCode.ToString()); message = " Registration successfully done. Account activation link " + " has been sent to your email: " + user.Email + "\nKindly click on the link to activate your account.\n" + " BookApp2020"; Status = true; } #endregion } else { message = "Invalid Request"; } ViewBag.Message = message; ViewBag.Status = Status; return(View(user)); }
public ActionResult VerifyAccount(string id) { bool Status = false; using (BookAppDbEntities dc = new BookAppDbEntities()) { dc.Configuration.ValidateOnSaveEnabled = false; var v = dc.Users.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault(); if (v != null) { v.IsEmailVerified = true; dc.SaveChanges(); Status = true; } else { ViewBag.Message = "Invalid Request"; } } ViewBag.Status = Status; return(View()); }
public ActionResult ResetPassword(string id) { if (string.IsNullOrWhiteSpace(id)) { return(HttpNotFound()); } using (BookAppDbEntities db = new BookAppDbEntities()) { var user = db.Users.Where(a => a.ResetPasswordCode == id).FirstOrDefault(); if (user != null) { ResetPasswordModel model = new ResetPasswordModel(); model.ResetCode = id; return(View(model)); } else { return(HttpNotFound()); } } }
public ActionResult Login(UserLogin login, string ReturnUrl = "") { if (!string.IsNullOrEmpty(login.EmailID)) { if (!string.IsNullOrEmpty(login.Password)) { if (ModelState.IsValid) { string message = ""; using (BookAppDbEntities dc = new BookAppDbEntities()) { var getUser = dc.Users.Where(a => a.Email == login.EmailID).FirstOrDefault(); Session["BookCount"] = dc.Books.ToList().Count(); if (getUser != null) { if (!getUser.IsEmailVerified) { ViewBag.Message = "Please verify your email first"; return(View()); } if (string.Compare(Crypto.Hash(login.Password), getUser.Password) == 0) { int timeout = login.RememberMe ? 525600 : 20; // 525600 min = 1 year var ticket = new FormsAuthenticationTicket(login.EmailID, login.RememberMe, timeout); string encrypted = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted); cookie.Expires = DateTime.Now.AddMinutes(timeout); cookie.HttpOnly = true; Response.Cookies.Add(cookie); if (Url.IsLocalUrl(ReturnUrl)) { return(Redirect(ReturnUrl)); } else { Session["FirstName"] = getUser.FirstName; Session["LastName"] = getUser.LastName; Session["ID"] = getUser.ID_Number; Session["Address"] = getUser.Address; Session["Phone"] = getUser.Phone; Session["Email"] = getUser.Email; return(RedirectToAction("WelcomePage", "Home")); } } else { message = "Invalid credential provided"; } } else { message = "Invalid credential provided"; } } ViewBag.Message = message; return(View()); } } else { ViewBag.Message = "Error: Password is a required field!"; } } else { ViewBag.Message = "Error: Username/Email is a required field!"; } return(View()); }