public async Task <ActionResult> GeneratePassword(string id) { ApplicationDbContext UsersContext = new ApplicationDbContext(); if (Session["traderID"] == null) { return(new HttpStatusCodeResult(5)); } int traderId = Convert.ToInt32(Session["traderID"].ToString()); var Db = new ApplicationDbContext(); ApplicationUser userObj = Db.Users.Where(j => j.TraderId == traderId && j.Id == id).First(); string createdPass = UsefulMethods.GenerateNewPassword(); PasswordHasher hasher = new PasswordHasher(); userObj.PasswordHash = hasher.HashPassword(createdPass); userObj.NeedReset = true; userObj.DefaultPassword = createdPass; //var result = await UserManager.UpdateAsync(userObj); Db.Entry(userObj).State = EntityState.Modified; await Db.SaveChangesAsync(); int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["DefaultPageSize"]); int pageNumber = 1; ViewBag.pageSize = pageSize; List <ApplicationUser> userList = UsersContext.Users.Where(j => j.TraderId == traderId).OrderBy(o => o.UserName).ToList(); return(PartialView("_List", userList.ToPagedList(pageNumber, pageSize))); }
public async Task <ActionResult> SendNewPassword(LoginViewModel model) { var user = await UserManager.FindByNameAsync(model.UserName); if (user != null) { int userType = uow.TraderRepo.FindTraderById(user.TraderId).TypeFlag; if (user != null && userType != 1) { if (!user.IsVerified || !user.IsActive) { ModelState.AddModelError("", !user.IsVerified ? Resources.Messages.VerifyAccount : Resources.Messages.Userinactive); return(View("ForgetPassword", model)); } string destination = user.Email; string generatedPW = UsefulMethods.GenerateNewPassword(); PasswordHasher hasher = new PasswordHasher(); string hashed = hasher.HashPassword(generatedPW); //user.DefaultPassword = generatedPW; user.PasswordHash = null; user.Token = Guid.NewGuid(); user.NeedReset = true; user.TokenCreationDate = DateTime.Now; UserManager.Update(user); model.Resend = false; model.UserName = General.CheckMail; //send new password mail . var callbackUrl = Url.Action("ForgotPassword", "Account", new { userName = user.UserName, code = user.Token }, protocol: Request.Url.Scheme); Mail objMail = new Mail { Destination = user.Email, Subject = "Forget Password", Body = GetBodyContent("كلمة السر الجديدة", "Please click this link to set your new password: <a href=\"" + callbackUrl + "\">link</a>") }; await UsefulMethods.SendAsync(objMail); return(View("DisplayEmail", model)); } else { ModelState.AddModelError("", userType == 1 ? Resources.Messages.SuperTraderType : Resources.Messages.UserInCorrect); return(View("ForgetPassword", model)); } } else { ModelState.AddModelError("", user == null ? Resources.Messages.verifyUserName : Resources.Messages.userNotExsist); return(View("ForgetPassword", model)); } }
public async Task <ActionResult> _Add(string userName) { if (Session["traderID"] == null) { return(new HttpStatusCodeResult(5)); } bool check = uow.UsersRepo.IsUserNameExist(userName); if (check == true) { return(new HttpStatusCodeResult(2)); } string generatedPW = UsefulMethods.GenerateNewPassword(); PasswordHasher hasher = new PasswordHasher(); string hashed = hasher.HashPassword(generatedPW); ApplicationDbContext UsersContext = new ApplicationDbContext(); ApplicationUser userObj = new ApplicationUser(); int traderId = Convert.ToInt32(Session["traderID"].ToString()); userObj.UserName = userName; userObj.TraderId = traderId; userObj.PasswordHash = generatedPW; //hashed; userObj.DefaultPassword = generatedPW; userObj.IsActive = true; userObj.NeedReset = true; userObj.IsVerified = true; userObj.TokenCreationDate = DateTime.Now; if (UsersContext.Users.Where(j => j.TraderId == traderId).ToList().Count() == 0) { userObj.MainUser = true; } else { userObj.MainUser = false; } IdentityManager im = new IdentityManager(); im.CreateUser(userObj, userObj.PasswordHash); //Assign Roles Trader traderObj = uow.TraderRepo.FindTraderById(traderId); List <RolePermission> listRoles = uow.RolePermissionRepo.GetRolePermissionsByTypeID(traderObj.TypeFlag); foreach (var role in listRoles) { ApplicationUserRole appUserRole = new ApplicationUserRole(); appUserRole.UserId = userObj.Id; appUserRole.RoleId = role.AspNetRolesID.ToString(); uow.RolePermissionRepo.AddNewUserRoles(appUserRole); } int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["DefaultPageSize"]); int pageNumber = 1; ViewBag.pageSize = pageSize; List <ApplicationUser> userList = UsersContext.Users.Where(j => j.TraderId == traderId).OrderBy(o => o.UserName).ToList(); return(PartialView("_List", userList.ToPagedList(pageNumber, pageSize))); }