/// <summary> /// This ,ethod is used to authenticate a users login /// </summary> /// <param name="email"></param> /// <param name="password"></param> /// <returns></returns> public Contact AuthenticateAppUserLogin(string email, string password) { var hashPassword = new Md5Ecryption().ConvertStringToMd5Hash(password.Trim()); var user = new ContactFactory().GetAppUserByLogin(email, hashPassword); return(user); }
/// <summary> /// This method is used to reset a user password /// </summary> /// <param name="newPassword"></param> /// <param name="userId"></param> public void ResetUserPassword(string newPassword, int userId) { var user = _db.Contacts.Find(userId); user.Password = newPassword; var hashPasword = new Md5Ecryption().ConvertStringToMd5Hash(newPassword); _db.Entry(user).State = EntityState.Modified; user.Password = hashPasword; _db.SaveChanges(); }
public ActionResult Create( [Bind( Include = "AppUserId,Firstname,Lastname,Othername,Email,MobileNumber,DepartmentId" )] AppUser appUser, FormCollection collectedValues) { if (ModelState.IsValid) { var loggedinuser = Session["courseshuffleloggedinuser"] as AppUser; if ((loggedinuser != null) && (loggedinuser.Role == UserType.Administrator.ToString())) { if (collectedValues["Role"] == null) { TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Danger.ToString(); return(View(appUser)); } var profileImage = Request.Files["avatar-2"]; appUser.DateCreated = DateTime.Now; appUser.DateLastModified = DateTime.Now; appUser.LastModifiedBy = loggedinuser.AppUserId; appUser.CreatedBy = loggedinuser.AppUserId; appUser.Role = typeof(UserType).GetEnumName(int.Parse(collectedValues["Role"])); var password = Membership.GeneratePassword(8, 1); var hashPassword = new Md5Ecryption().ConvertStringToMd5Hash(password.Trim()); appUser.Password = new RemoveCharacters().RemoveSpecialCharacters(hashPassword); appUser.ProfilePicture = new FileUploader().UploadFile(profileImage, UploadType.ProfileImage.ToString()); _db.AppUsers.Add(appUser); var userExist = new AppUserFactory().CheckIfGeneralUserExist(appUser.Email.Trim()); if (userExist) { TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Danger.ToString(); return(View(appUser)); } _db.SaveChanges(); appUser.Password = password; new MailerDaemon().NewUser(appUser); TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Success.ToString(); return(RedirectToAction("Index")); } TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Info.ToString(); return(RedirectToAction("Index")); } ViewBag.DepartmentId = new SelectList(_db.Departments, "DepartmentId", "Name"); return(View(appUser)); }
public ActionResult ChangePassword(FormCollection collectedValues) { var oldPassword = collectedValues["OldPassword"]; var newPassword = collectedValues["NewPassword"]; var confirmPassword = collectedValues["ConfirmNewPassword"]; var loggedinuser = Session["bhuinfologgedinuser"] as AppUser; if (ModelState.IsValid) { if (newPassword == confirmPassword) { var hashPassword = new Md5Ecryption().ConvertStringToMd5Hash(oldPassword.Trim()); if ((loggedinuser != null) && (hashPassword == loggedinuser.Password)) { if (new AuthenticationFactory().ChangeUserPassword(Convert.ToInt64(loggedinuser.AppUserId), oldPassword, newPassword)) { TempData["password"] = "******"; TempData["notificationtype"] = NotificationType.Success.ToString(); Session["bhuinfologgedinuser"] = null; return(RedirectToAction("Login", "Account")); } } else { TempData["password"] = "******"; TempData["notificationtype"] = NotificationType.Danger.ToString(); return(View("ChangePassword")); } } else { TempData["password"] = "******"; TempData["notificationtype"] = NotificationType.Info.ToString(); return(View("ChangePassword")); } } return(View()); }
public ActionResult Register([Bind(Include = "Firstname,Lastname,Email,Mobile,Password")] AppUser appUser, FormCollection collectedValues) { var loggedinuser = Session["bhuinfologgedinuser"] as AppUser; HttpPostedFileBase profileImage = Request.Files["avatar-2"]; if (ModelState.IsValid) { if (collectedValues["student"] == null) { if ((loggedinuser != null) && (loggedinuser.Role == UserType.Administrator.ToString())) { appUser.DateCreated = DateTime.Now; appUser.DateLastModified = DateTime.Now; appUser.CreatedById = loggedinuser.AppUserId; appUser.LastModifiedById = loggedinuser.AppUserId; appUser.Role = typeof(UserType).GetEnumName(int.Parse(collectedValues["Role"])); var password = Membership.GeneratePassword(8, 1); var hashPassword = new Md5Ecryption().ConvertStringToMd5Hash(password.Trim()); appUser.Password = new RemoveCharacters().RemoveSpecialCharacters(hashPassword); appUser.AppUserImage = new FileUploader().UploadFile(profileImage, UploadType.ProfileImage); var userExist = new AppUserFactory().CheckIfStudentUserExist(appUser.Email.Trim(), appUser.MatricNumber.Trim()); if (userExist) { TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Danger.ToString(); return(View(appUser)); } _db.AppUsers.Add(appUser); _db.SaveChanges(); TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Success.ToString(); appUser.Password = password; new MailerDaemon().NewUser(appUser); } else { TempData["user"] = "******"; TempData["notificationtype"] = NotificationType.Info.ToString(); return(RedirectToAction("Index")); } } else { appUser.DateCreated = DateTime.Now; appUser.DateLastModified = DateTime.Now; appUser.CreatedById = 1; appUser.MatricNumber = collectedValues["MatricNumber"].Trim(); appUser.LastModifiedById = 1; appUser.Role = UserType.Student.ToString(); var password = Membership.GeneratePassword(8, 1); var hashPassword = new Md5Ecryption().ConvertStringToMd5Hash(password.Trim()); appUser.AppUserImage = new FileUploader().UploadFile(profileImage, UploadType.ProfileImage); appUser.Password = new RemoveCharacters().RemoveSpecialCharacters(hashPassword); var userExist = new AppUserFactory().CheckIfGeneralUserExist(appUser.Email.Trim()); if (userExist) { TempData["student"] = "This user email already exist,try a different email!"; TempData["notificationtype"] = NotificationType.Danger.ToString(); return(View(appUser)); } _db.AppUsers.Add(appUser); _db.SaveChanges(); TempData["student"] = "You have been created on bhuinfo!"; TempData["notificationtype"] = NotificationType.Success.ToString(); appUser.Password = password; new MailerDaemon().NewUser(appUser); return(RedirectToAction("Login", "Account")); } return(RedirectToAction("Login", "Account")); } return(View(appUser)); }
public IActionResult Create(AppUser appUser, IFormFile ProfilePicture) { var authorizedUser = new AppUser(); if (HttpContext.Session.GetString("FrscQuestionLoggedInUser") != null) { var userString = HttpContext.Session.GetString("FrscQuestionLoggedInUser"); authorizedUser = JsonConvert.DeserializeObject <AppUser>(userString); } if (!authorizedUser.Role.AccessAdminConsole || !authorizedUser.Role.ManageApplicationUser) { return(RedirectToAction("UnauthorizedAccess", "Home")); } try { var signedInUserId = Convert.ToInt64(HttpContext.Session.GetString("FrscQuestionLoggedInUserId")); appUser.CreatedBy = signedInUserId; appUser.LastModifiedBy = signedInUserId; appUser.DateCreated = DateTime.Now; appUser.DateLastModified = DateTime.Now; appUser.HasSocialMediaLogin = false; appUser.Status = UserStatus.Inactive.ToString(); appUser.AccountType = LoginType.Platform.ToString(); appUser.HasSocialMediaLogin = false; //generate password var password = new Md5Ecryption().RandomString(8); appUser.Password = new Hashing().HashPassword(password); appUser.ConfirmPassword = appUser.Password; if (_databaseConnection.AppUsers.Where(n => n.Email == appUser.Email).ToList().Count > 0) { ViewBag.RoleId = new SelectList(_databaseConnection.Roles.ToList(), "RoleId", "Name", appUser.RoleId); TempData["display"] = "A user with the same email already exist!"; TempData["notificationtype"] = NotificationType.Error.ToString(); return(View(appUser)); } //upload user logo if any file is uploaded if (ProfilePicture != null && !string.IsNullOrEmpty(ProfilePicture.FileName)) { var fileInfo = new FileInfo(ProfilePicture.FileName); var ext = fileInfo.Extension.ToLower(); var name = DateTime.Now.ToFileTime().ToString(); var fileName = name + ext; var uploadedImage = _hostingEnv.WebRootPath + $@"\UploadedFiles\ProfilePicture\{fileName}"; using (var fs = System.IO.File.Create(uploadedImage)) { if (fs != null) { ProfilePicture.CopyTo(fs); fs.Flush(); appUser.ProfilePicture = fileName; } } } _databaseConnection.AppUsers.Add(appUser); _databaseConnection.SaveChanges(); if (appUser.AppUserId > 0) { //define acceskeys and save transactions var accessKey = new AppUserAccessKey { PasswordAccessCode = new Md5Ecryption().RandomString(15), AccountActivationAccessCode = new Md5Ecryption().RandomString(20), CreatedBy = appUser.AppUserId, LastModifiedBy = appUser.AppUserId, DateCreated = DateTime.Now, DateLastModified = DateTime.Now, ExpiryDate = DateTime.Now.AddDays(1), AppUserId = appUser.AppUserId }; _databaseConnection.AppUserAccessKeys.Add(accessKey); _databaseConnection.SaveChanges(); new Mailer().SendNewUserEmail(new AppConfig().NewUserHtml, appUser, accessKey); } TempData["display"] = "You have successfully added a new user!"; TempData["notificationtype"] = NotificationType.Success.ToString(); return(RedirectToAction("Index")); } catch (Exception ex) { ViewBag.RoleId = new SelectList(_databaseConnection.Roles.ToList(), "RoleId", "Name", appUser.RoleId); //display notification TempData["display"] = ex.Message; TempData["notificationtype"] = NotificationType.Error.ToString(); return(View(appUser)); } }