Пример #1
0
        public async Task <IActionResult> Create([Bind("Id,UserName,Password,IsActive,RegisterDate")] CUsers users)
        {
            if (ModelState.IsValid)
            {
                string result = await _userRep.AnyUser‍Insert(users);

                if (result != "True")
                {
                    users.RegisterDate = DateTime.Now;

                    //Hash Password
                    users.Password = HashPassword.ToHashPassword(users.Password);

                    await _userRep.InsertUser(users);

                    await _userRep.Save();

                    Success();
                    return(RedirectToAction(nameof(Index)));
                }

                ModelState.AddModelError("UserName", " اين نام كاربري قبلاً در سيستم ثبت شده است");
            }
            RegisterViewModel viewModelUser = new RegisterViewModel();

            viewModelUser.UserName = users.UserName;
            viewModelUser.IsActive = users.IsActive;
            viewModelUser.Password = users.Password;
            return(View(viewModelUser));
        }
Пример #2
0
        public async Task <IActionResult> ChangePassword(int id, ChangePasswordViewModel users)
        {
            if (id != users.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //Hash Password
                    users.Password = HashPassword.ToHashPassword(users.Password);

                    await _userRep.ChangePassword(users);

                    await _userRep.Save();

                    Success();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CUsersExists(users.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(users));
        }
Пример #3
0
        public async Task <IActionResult> ChangePasswordUser(ChangePasswordUserViewModel users)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = await _userRep.GetByUserName(User.Identity.Name);

                    string password = HashPassword.ToHashPassword(users.OldPassword);
                    if (user.Password == password)
                    {
                        //Hash Password
                        user.Password = HashPassword.ToHashPassword(users.Password);
                        await _userRep.ChangePasswordUser(user);

                        await _userRep.Save();

                        //Success();
                    }
                    else
                    {
                        ModelState.AddModelError("OldPassword", "رمز عبور فعلي نادرست است");
                        return(View(users));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(NotFound());
                }
                return(RedirectToAction("Index", "Home"));
            }
            return(View(users));
        }
Пример #4
0
        public async Task <IActionResult> Login(LoginViewModel login, string ReturnUrl = "/")
        {
            if (ModelState.IsValid)
            {
                //Hash Password
                login.Password = HashPassword.ToHashPassword(login.Password);

                string result = await _accountRep.Login(login);

                if (result == "Success")
                {
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
                    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, login.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.Name, login.UserName));
                    var principal = new ClaimsPrincipal(identity);
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties
                    {
                        IsPersistent = false
                    });

                    return(RedirectToAction("Index", "Home"));
                }
                if (result == "NotActive")
                {
                    ModelState.AddModelError("UserName", "حساب کاربری شما غير فعال است");
                }

                if (result == "NotFound")
                {
                    ModelState.AddModelError("UserName", "کاربری با اطلاعات وارد شده یافت نشد");
                }
            }

            return(View(login));
        }