public ActionResult Login(LoginUserVM model) { //Check model state if (!ModelState.IsValid) { return(View(model)); } //Set bool isValid for checker bool isValid = false; bool confirmBool = false; //Call DbSet using (Db db = new Db()) { var user = db.Users.SingleOrDefault(x => x.EmailAddress.Equals(model.EmailAddress)); if (user != null) { //Fetch stored password by user var samplePassword = user.Password; //Verify Password CustomPasswordHasher hash = new CustomPasswordHasher(); var prodPassword = hash.HashPassword(model.Password); if (samplePassword.Equals(prodPassword)) { isValid = true; } else { isValid = false; } } if (user.EmailConfirm == confirmBool) { TempData["Failure"] = "Please activate your account via email"; //ModelState.AddModelError("", "Invalid Username or Password"); return(View(model)); } } //Check isValid is true if (!isValid) { TempData["Failure"] = "Invalid Username or Password"; //ModelState.AddModelError("", "Invalid Username or Password"); return(View(model)); } else { //Set a cookie or session for the user FormsAuthentication.SetAuthCookie(model.EmailAddress, model.RememberMe); return(Redirect(FormsAuthentication.GetRedirectUrl(model.EmailAddress, model.RememberMe))); } }
public ActionResult Reset(ResetPasswordVM model) { if (RouteData.Values["id"] != null) { //Check model state if (!ModelState.IsValid) { return(View("Reset", model)); } Guid resetCode = new Guid(RouteData.Values["id"].ToString()); using (Db db = new Db()) { //Check if passwords match if (!model.Password.Equals(model.ConfirmPassword)) { TempData["Failure"] = "Password do not match"; return(View("Reset", model)); } //Hash the Password CustomPasswordHasher hash = new CustomPasswordHasher(); string hashedPassword = hash.HashPassword(model.Password); var entity = db.Users.Where(x => x.ResetCode == resetCode.ToString()).FirstOrDefault(); if (entity != null) { entity.ResetCode = ""; entity.Password = hashedPassword; } db.SaveChanges(); ViewBag.Message = "Account Successfully Resetted"; return(RedirectToAction("ResetAck")); } } else { return(Redirect("~/Account/Login")); } }
public ActionResult EditSSH(DeleteSSHVM model) { string SSH = @"c:\temp\ssh.bat"; string Del = @"c:\temp\delete.bat"; using (Db db = new Db()) { if (db.SSHRecords.Any(x => x.Id.Equals(model.Id))) { string OldUser; string OldPass; SSHRecordDTO dto = db.SSHRecords.Find(model.Id); OldUser = dto.SSHUser; if (!string.IsNullOrEmpty(model.SSHPassword)) { CustomPasswordHasher hash = new CustomPasswordHasher(); string hashedPassword = hash.HashPassword(model.SSHPassword); if (hashedPassword.Equals(dto.SSHPassword)) { OldPass = model.SSHPassword; if (!System.IO.File.Exists(SSH)) { // Create a file to write to. using (StreamWriter sw = System.IO.File.CreateText(SSH)) { sw.WriteLine("en"); sw.WriteLine("cisco"); sw.WriteLine(""); sw.WriteLine("conf t"); sw.WriteLine("no username " + OldUser + " password " + OldPass); sw.WriteLine("enable password " + "cisco"); sw.WriteLine("exit"); sw.WriteLine("exit"); sw.WriteLine("exit"); } } } else { TempData["Failure"] = "Password does not match your SSH account"; return(View("DeleteSSH", model)); } } db.SSHRecords.Remove(dto); db.SaveChanges(); } } if (!System.IO.File.Exists(Del)) { // Create a file to write to. using (StreamWriter sw = System.IO.File.CreateText(Del)) { sw.WriteLine("@echo off"); sw.WriteLine("del ssh.bat"); sw.WriteLine("del delete.bat"); } } //Provision Account here string com = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PuTTY (64-bit)"; string com2 = @"putty.exe -ssh " + "admin" + "@" + "40.122.27.77" + " -pw " + "cisco" + " -m " + SSH; string command = "/C cd " + com + " & @echo off & " + com2 + " & cd.. & cd.. & cd.. & cd.. & cd.. & cd.. & cd C:/temp/ & @echo off & delete.bat"; System.Diagnostics.Process.Start("cmd.exe", command); TempData["Success"] = "You have successfully deleted the SSH Account"; return(RedirectToAction("ssh-record")); }
public ActionResult EditSSH(SSHRecordsVM model) { if (!ModelState.IsValid) { return(View("EditSSH", model)); } //Check if password is not empty if (!string.IsNullOrEmpty(model.SSHPassword)) { //Check if password and confirm password matches if (!model.SSHPassword.Equals(model.SSHPasswordConfirm)) { TempData["Failure"] = "Passwords do not match"; return(View("EditSSH", model)); } } using (Db db = new Db()) { if (db.SSHRecords.Any(x => x.Id.Equals(model.Id))) { SSHRecordDTO dto = db.SSHRecords.Find(model.Id); dto.SSHUser = model.SSHUser; if (!string.IsNullOrEmpty(model.SSHPassword)) { CustomPasswordHasher hash = new CustomPasswordHasher(); string hashedPassword = hash.HashPassword(model.SSHPassword); if (model.SSHPassword.Equals(model.SSHPasswordConfirm)) { dto.SSHPassword = hashedPassword; } } db.SaveChanges(); } else { TempData["Failure"] = "Invalid Edit SSH Request"; return(View("EditSSH", model)); } } string SSH = @"c:\temp\ssh.bat"; string Del = @"c:\temp\delete.bat"; if (!System.IO.File.Exists(SSH)) { // Create a file to write to. using (StreamWriter sw = System.IO.File.CreateText(SSH)) { sw.WriteLine("en"); sw.WriteLine("cisco"); sw.WriteLine(""); sw.WriteLine("conf t"); sw.WriteLine("username " + model.SSHUser + " password " + model.SSHPassword); sw.WriteLine("exit"); sw.WriteLine("exit"); sw.WriteLine("exit"); } } if (!System.IO.File.Exists(Del)) { // Create a file to write to. using (StreamWriter sw = System.IO.File.CreateText(Del)) { sw.WriteLine("@echo off"); sw.WriteLine("del ssh.bat"); sw.WriteLine("del delete.bat"); } } //Provision Account here string com = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PuTTY (64-bit)"; string com2 = @"putty.exe -ssh " + "admin" + "@" + "40.122.27.77" + " -pw " + "cisco" + " -m " + SSH; string command = "/C cd " + com + " & @echo off & " + com2 + " & cd.. & cd.. & cd.. & cd.. & cd.. & cd.. & cd C:/temp/ & @echo off & delete.bat"; System.Diagnostics.Process.Start("cmd.exe", command); TempData["Success"] = "You have successfully edited your SSH Account"; return(View("EditSSH")); }
public ActionResult CreateSSH(SSHRecordsVM model) { //Check if Mode State is valid if (!ModelState.IsValid) { return(View("CreateSSH", model)); } //Hash the Password CustomPasswordHasher hash = new CustomPasswordHasher(); string hashedPassword = hash.HashPassword(model.SSHPassword); //Check if password is not empty if (!string.IsNullOrEmpty(model.SSHPassword)) { //Check if password and confirm password matches if (!model.SSHPassword.Equals(model.SSHPasswordConfirm)) { TempData["Failure"] = "Passwords do not match"; return(View("CreateSSH", model)); } } using (Db db = new Db()) { //Make sure username is unique if (db.SSHRecords.Any(x => x.SSHUser.Equals(model.SSHUser))) { TempData["Failure"] = "Email Address " + model.SSHUser + " has already been created."; model.SSHUser = ""; return(View("CreateSSH", model)); } //Continue with the booking SSHRecordDTO sshRecordDTO = new SSHRecordDTO() { SSHUser = model.SSHUser, SSHPassword = hashedPassword }; db.SSHRecords.Add(sshRecordDTO); db.SaveChanges(); string SSH = @"c:\temp\ssh.bat"; string Del = @"c:\temp\delete.bat"; if (!System.IO.File.Exists(SSH)) { // Create a file to write to. using (StreamWriter sw = System.IO.File.CreateText(SSH)) { sw.WriteLine("en"); sw.WriteLine("cisco"); sw.WriteLine(""); sw.WriteLine("conf t"); sw.WriteLine("username " + model.SSHUser + " password " + model.SSHPassword); sw.WriteLine("exit"); sw.WriteLine("exit"); } } if (!System.IO.File.Exists(Del)) { // Create a file to write to. using (StreamWriter sw = System.IO.File.CreateText(Del)) { sw.WriteLine("@echo off"); sw.WriteLine("del ssh.bat"); sw.WriteLine("del delete.bat"); } } //Provision Account here string com = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PuTTY (64-bit)"; string com2 = @"putty.exe -ssh " + "admin" + "@" + "40.122.27.77" + " -pw " + "cisco" + " -m " + SSH; string command = "/C cd " + com + " & @echo off & " + com2 + " & cd.. & cd.. & cd.. & cd.. & cd.. & cd.. & cd C:/temp/ & @echo off & delete.bat"; System.Diagnostics.Process.Start("cmd.exe", command); TempData["Success"] = "You have successfully added and provisioned an SSH Account"; //string command2 = "/C cd C:/temp/ & @echo off & delete.bat"; //System.Diagnostics.Process.Start("cmd.exe", command2); return(RedirectToAction("ssh-record", "Booking")); } }
public ActionResult UserProfile(UserProfileVM model) { //Check model state if (!ModelState.IsValid) { return(View("UserProfile", model)); } //Check if password is not empty if (!string.IsNullOrEmpty(model.Password)) { //Check if password and confirm password matches if (!model.Password.Equals(model.ConfirmPassword)) { TempData["Failure"] = "Passwords do not match"; return(View("UserProfile", model)); } } //using DbSet using (Db db = new Db()) { //Get email address string emailAddress = User.Identity.Name; //Check if username is unique if (db.Users.Where(x => x.Id != model.Id).Any(x => x.EmailAddress == emailAddress)) { TempData["Failure"] = "Username is already taken"; model.EmailAddress = ""; return(View("UserProfile", model)); } //Edit DTO UserDTO dto = db.Users.Find(model.Id); dto.FirstName = model.FirstName; dto.LastName = model.LastName; dto.EmailAddress = model.EmailAddress; dto.PhoneNumber = model.PhoneNumber; dto.Department = model.Department; dto.ModifiedOn = DateTime.Now; if (!string.IsNullOrEmpty(model.Password)) { CustomPasswordHasher hash = new CustomPasswordHasher(); string hashedPassword = hash.HashPassword(model.Password); if (model.Password.Equals(model.ConfirmPassword)) { dto.Password = hashedPassword; } } //Save Changes db.SaveChanges(); } //Set Temp Message TempData["Success"] = "You have successfully updated your profile"; //Redirect return(Redirect("~/Account/user-profile")); }
public ActionResult CreateAccount(UserVM model) { //Check model state if (!ModelState.IsValid) { return(View("CreateAccount", model)); } //Check if passwords match if (!model.Password.Equals(model.ConfirmPassword)) { TempData["Failure"] = "Passwords do not match"; return(View("CreateAccount", model)); } //Hash the Password CustomPasswordHasher hash = new CustomPasswordHasher(); string hashedPassword = hash.HashPassword(model.Password); using (Db db = new Db()) { //Make sure username is unique if (db.Users.Any(x => x.EmailAddress.Equals(model.EmailAddress))) { TempData["Failure"] = "Email Address " + model.EmailAddress + " is already registered."; model.EmailAddress = ""; return(View("CreateAccount", model)); } //Create a activation GUID Guid activationCode = Guid.NewGuid(); //Create userDTO UserDTO userDTO = new UserDTO() { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, PhoneNumber = model.PhoneNumber, Department = model.Department, Password = hashedPassword, EmailConfirm = false, ActivationCode = activationCode.ToString(), CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now }; //Add userDTO db.Users.Add(userDTO); //Save DTO db.SaveChanges(); //Add to userRoleDTO int userId = userDTO.Id; UserRoleDTO userRoleDTO = new UserRoleDTO() { UserId = userId, RoleId = 2 }; db.UserRoles.Add(userRoleDTO); db.SaveChanges(); //Mail Message using (MailMessage mm = new MailMessage()) { mm.From = new MailAddress("*****@*****.**"); mm.To.Add(model.EmailAddress); mm.Subject = "Account Activation For Dexter Lab"; string body = "Hello " + model.FirstName + " " + model.LastName + ","; body += "<br /><br />Please click the following link to activate your account"; body += "<br /><a href = '" + string.Format("{0}://{1}/Account/Activation/{2}", Request.Url.Scheme, Request.Url.Authority, activationCode) + "'>Click here to activate your account.</a>"; body += "<br /><br />Thanks"; body += "<br />NTT Dexter Lab"; mm.Body = body; mm.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)) { smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]); smtp.EnableSsl = true; smtp.Send(mm); } } } //Create a tempdata message TempData["Success"] = "You have successfully registered your account. Check your email to activate your account before logging in."; //Redirect return(Redirect("~/Account/login")); }