public bool ResetPassword(string code, string password) { User user = _context.Users.FirstOrDefault(u => u.ActiveCode == code); if (user != null) { user.Password = HashGenerators.MD5Encoding(password); user.ActiveCode = CodeGenerators.ActiveCode(); _context.SaveChanges(); return(true); } else { return(false); } }
public IActionResult Register(RegisterViewModel viewModel) { if (ModelState.IsValid) { if (_account.ExistsMobileNumber(viewModel.Mobile)) { // Go To Login } else { User user = new User() { Mobile = viewModel.Mobile, ActiveCode = CodeGenerators.ActiveCode(), Code = null, Date = pc.GetYear(DateTime.Now).ToString("0000") + "/" + pc.GetMonth(DateTime.Now).ToString("00") + "/" + pc.GetDayOfMonth(DateTime.Now).ToString("00"), FullName = null, IsActive = false, Password = HashGenerators.MD5Encoding(viewModel.Password), RoleId = _account.GetMaxRole() }; _account.AddUser(user); try { MessageSender sender = new MessageSender(); sender.SMS(viewModel.Mobile, "به فروشگاه اینترنتی خوش آمدید" + Environment.NewLine + "کد فعالسازی : " + user.ActiveCode); } catch { } return(RedirectToAction(nameof(Activate))); } } return(View(viewModel)); }
public IActionResult Store(StoreRegisterViewModel viewModel) { if (ModelState.IsValid) { if (_account.ExistsMailAddress(viewModel.Mail)) { ViewBag.MyMessage = false; ModelState.AddModelError("Mail", "نمی توانید از این ایمیل استفاده کنید"); } else { int userID = 0; string mobileCode = ""; if (_account.ExistsMobileNumber(viewModel.Mobile)) { _account.UpdateUserRole(viewModel.Mobile); userID = _account.GetUserId(viewModel.Mobile); mobileCode = _account.GetUserActiveCode(viewModel.Mobile); } else { mobileCode = CodeGenerators.ActiveCode(); User user = new User() { ActiveCode = mobileCode, Code = null, FullName = null, IsActive = false, Mobile = viewModel.Mobile, Password = HashGenerators.MD5Encoding(viewModel.Password), Date = pc.GetYear(DateTime.Now).ToString("0000") + "/" + pc.GetMonth(DateTime.Now).ToString("00") + "/" + pc.GetDayOfMonth(DateTime.Now).ToString("00"), RoleId = _account.GetStoreRole() }; _account.AddUser(user); userID = user.Id; } Store store = new Store() { Address = null, Desc = null, Logo = null, Mail = viewModel.Mail, MailActivate = false, MobileActivate = false, Tel = null, UserId = userID, Name = null, MailActivateCode = CodeGenerators.ActiveCode() }; _account.AddStore(store); ViewBag.MyMessage = true; MessageSender sender = new MessageSender(); string messageBody = _render.RenderToStringAsync("_ActivateMail", store); try { sender.Email(store.Mail, "فعالسازی فروشگاه", messageBody); //sender.SMS(viewModel.Mobile, "درخواست ثبت فروشگاه انجام شد" + Environment.NewLine + "کد فعالسازی : " + mobileCode); } catch { } } } return(View(viewModel)); }
public IActionResult Login(LoginViewModel viewModel) { if (ModelState.IsValid) { string hashPassword = HashGenerators.MD5Encoding(viewModel.Password); User user = _account.LoginUser(viewModel.Mobile, hashPassword); if (user != null) { if (user.Role.Name == "فروشگاه") { var claims = new List <Claim>() { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Mobile) }; var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var principal = new ClaimsPrincipal(identity); var properties = new AuthenticationProperties() { IsPersistent = true }; HttpContext.SignInAsync(principal, properties); return(RedirectToAction("Dashboard", "Panel")); } else { if (user.IsActive) { var claims = new List <Claim>() { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Mobile) }; var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var principal = new ClaimsPrincipal(identity); var properties = new AuthenticationProperties() { IsPersistent = true }; HttpContext.SignInAsync(principal, properties); if (user.Role.Name == "کاربر") { return(RedirectToAction("Dashboard", "Home")); } else { return(RedirectToAction("Dashboard", "Panel")); } } else { return(RedirectToAction(nameof(Activate))); } } } else { ModelState.AddModelError("Password", "مشخصات کاربری اشتباه است"); } } return(View(viewModel)); }