public ActionResult VerifyAccount(string id) { bool Status = false; using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //Gets or sets a value indicating whether tracked entities should be validated automatically //when SaveChanges() is invoked. //The default value is true //Temporarily turn off the model validation by setting ValidateOnSaveEnabled of context to false db.Configuration.ValidateOnSaveEnabled = false; //Verify if Activation Code is equal to the GUID or Global Unique Identifier of our string var x = db.Users.Where(model => model.ActivationCode == new Guid(id)).FirstOrDefault(); if (x != null) { //if value of x is not null, then update IsEmailVerified to true or 1 in our database x.IsEmailVerified = true; db.SaveChanges(); Status = true; } else { ViewBag.Message = "Invalid request"; } } ViewBag.Status = Status; return(View()); }
public ActionResult AddOrEdit(Employee emp) { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //use for adding and updating new employee if (emp.Id == 0) { //add the values from employee model, to employees table db.Employees.Add(emp); //save db changes db.SaveChanges(); //return now the view, with success message for adding successfully. return(Json(new { success = true, message = "Saved successfully", JsonRequestBehavior.AllowGet })); } else { //update entry, set db entity state to modified db.Entry(emp).State = EntityState.Modified; //save changes db.SaveChanges(); //return view with success message return(Json(new { success = true, message = "Updated successfully", JsonRequestBehavior.AllowGet })); } } }
public ActionResult ResetPassword(ResetPasswordModel model) { var message = ""; //Verify if there's no error if (ModelState.IsValid) { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //Check if ResetPasswordCode is equal to Reset Code var user = db.Users.Where(a => a.ResetPasswordCode == model.ResetCode).FirstOrDefault(); //If var user reset pass code is not null if (user != null) { //Hash new password user.Password = PasswordHash.Hash(model.NewPassword).ToUpper(); //Clear now ResetPasswordCode user.ResetPasswordCode = ""; db.Configuration.ValidateOnSaveEnabled = false; //Save changes db.SaveChanges(); message = "Your password updated successfully."; } } } else { message = "You password is invalid"; } ViewBag.Message = message; return(View(model)); }
public ActionResult ResetPassword(string id) { bool status = false; using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { var userpass = db.Users.Where(model => model.ResetPasswordCode == id).FirstOrDefault(); //Validate if reset password code is not null if (userpass != null) { //Model for New Password, confirm password and resetcode ResetPasswordModel model = new ResetPasswordModel(); //Pass resetpasswordcode from db to reset code model.ResetCode = id; status = true; return(View(model)); } else { ViewBag.Status = status; //if useprass is not valid, set httpnotfoundresult class return(HttpNotFound()); } } }
public ActionResult ForgotPassword(string Email) { string message = ""; bool status = false; using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //using our db context, verify if input email exists var account = db.Users.Where(model => model.Email == Email).FirstOrDefault(); if (account != null) { //Send email for resetting password //Sending new Global Unique Identifier string resetCode = Guid.NewGuid().ToString(); //Method for sending email verification, code for reset passowrd, then the view. SendEmailVerification(account.Email, resetCode, "ResetPassword"); account.ResetPasswordCode = resetCode; //Temporarily turn off the model validation db.Configuration.ValidateOnSaveEnabled = false; db.SaveChanges(); //Clear input field ModelState.Clear(); status = true; message = "Reset password link has been sent to your email."; } else { message = "Account not found"; } } ViewBag.Status = status; ViewBag.Message = message; return(View()); }
public Boolean IsEmailExist(string email) { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //Check db context if input email is already exists. var x = db.Users.Where(model => model.Email == email).FirstOrDefault(); return(x != null); } }
//Exclude the two fields for registration public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user) { bool Status = false; string message = ""; //Model Validation //If all the input fields is valid if (ModelState.IsValid) { #region "IF Email already exist" //Validate if email already exist var emailexist = IsEmailExist(user.Email); if (emailexist) { //Add error to my ValidationMessage named "EmailExist" and add error message." ModelState.AddModelError("EmailExist", "Email already exist."); return(View(user)); } #endregion #region "Generate activation code" //Generate a Global Unique Identifier or GUID for email activation code user.ActivationCode = Guid.NewGuid(); #endregion #region "Hash Password" //Password and confirm password Hashing user.Password = PasswordHash.Hash(user.Password).ToUpper(); user.ConfirmPassword = PasswordHash.Hash(user.ConfirmPassword).ToUpper(); #endregion //Set EmailVerified to false which it's equivalent in our SSMS boolean datataype is "0" user.IsEmailVerified = false; #region "Save to database" //The finally, save to database. using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { db.Users.Add(user); db.SaveChanges(); //Send email to user for account activation SendEmailVerification(user.Email, user.ActivationCode.ToString()); message = "Successfully registered! Please go to your email to activate you account: " + user.Email; //Set status to true to enable Success Message on our view Status = true; } #endregion } else { message = "Invalid Request"; } ViewBag.Message = message; ViewBag.Status = Status; return(View(user)); }
public ActionResult GetData() { //Retrieve now the data from the database using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //Create now the an employee list //return db.employees list to emplist List <Employee> empList = db.Employees.ToList <Employee>(); //return a json or JavaScript Object Notation view //allow http get requests //pass emplist to json data. return(Json(new { data = empList }, JsonRequestBehavior.AllowGet)); } }
public ActionResult Delete(int id) { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //find the id of the row you want to delete. Employee emp = db.Employees.Where(x => x.Id == id).FirstOrDefault <Employee>(); //remove db.Employees.Remove(emp); //then save changes from the db db.SaveChanges(); //return view with success message return(Json(new { success = true, message = "Deleted successfully", JsonRequestBehavior.AllowGet })); } }
public ActionResult CurrentUser(string loggeduser) { if (Session["Email"] == null) { return(RedirectToAction("Login", "Authentication")); } else { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { loggeduser = Session["Email"].ToString(); return(View(db.Users.Where(x => x.Email == loggeduser).FirstOrDefault <User>())); } //List<User> userList = db.Users.ToList<User>(); //return Json(new { data = userList }, JsonRequestBehavior.AllowGet); } }
public ActionResult AddOrEdit(int id = 0) { //If sessions is null, redirect to login if (Session["Email"] == null) { return(RedirectToAction("Login", "Authentication")); } //if not, //if id = 0, retrieve employee table if (id == 0) { return(View(new Employee())); } else { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { //if not, id = 0, retrieve the corresponding row with corresponding id return(View(db.Employees.Where(x => x.Id == id).FirstOrDefault <Employee>())); } } }
//Boolean method if email is verified or not public bool VerifiedEmail(string email) { //bool Status = false; //string message = ""; using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { db.Configuration.ValidateOnSaveEnabled = false; var x = db.Users.Where(model => model.Email == email).FirstOrDefault(); if (x != null) { if (x.IsEmailVerified == false) { //Return false if not yet verified return(false); } } else { ViewBag.Message = "Invalid request"; } } //Else return true return(true); }
public ActionResult Login(UserLogin login, string ReturnURL = "") { string message = ""; //Verify if email already verified if (VerifiedEmail(login.Email) == true) { using (EmployeeDBEntitiesEntities db = new EmployeeDBEntitiesEntities()) { var x = db.Users.Where(model => model.Email == login.Email).FirstOrDefault(); //if db context contains the input email if (x != null) { //Hash the model password then compare if to the db context password if (string.Compare(PasswordHash.Hash(login.Password).ToUpper(), x.Password) == 0) { //**Pluralsight demo //Set ClaimsIdentity which Authetication type is "ApplicationCookie" var identity = new ClaimsIdentity("ApplicationCookie"); identity.AddClaims(new List <Claim> { //Add the value for ApplicationCookie if email and password is valid new Claim(ClaimTypes.NameIdentifier, login.Email), new Claim(ClaimTypes.Name, login.Email) }); //Sign in identity HttpContext.GetOwinContext().Authentication.SignIn(identity); //**If remember me is clicked //1800s = 30 mins int timeout = login.RememberMe ? 1800 : 20; var ticket = new FormsAuthenticationTicket(login.Email, login.RememberMe, timeout); string encrpyt = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrpyt); cookie.Expires = DateTime.Now.AddMinutes(timeout); // cookie not available in javascript cookie.HttpOnly = true; Response.Cookies.Add(cookie); if (Url.IsLocalUrl(ReturnURL)) { return(Redirect(ReturnURL)); } else { //Add Session Session["Email"] = login.Email; return(RedirectToAction("Index", "Employee")); } } else { message = "Invalid Email or Password."; } } else { message = "Invalid Email or Password."; } } ViewBag.Message = message; return(View()); } else { //If email is not yet activated message = "You account is not yet activated."; ViewBag.Message = message; } return(View()); }