Beispiel #1
0
        public async Task <IActionResult> Login(AccountLoginModelView model)
        {
            if (ModelState.IsValid)
            {
                User user = await _userManager.FindByEmailAsync(model.Authetificator);

                if (user is null)
                {
                    user = await _userManager.FindByNameAsync(model.Authetificator);
                }
                if (user is null)
                {
                    ModelState.AddModelError("", "Не корректный пароль и(или) аутентификатор");
                    return(View(model));
                }
                Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync(
                    user,
                    model.Password,
                    true,
                    false
                    );

                if (result.Succeeded)
                {
                    _userService.AddUserToCache(user);
                    return(RedirectToAction("Details"));
                }
                ModelState.AddModelError("", "Не корректный пароль и(или) аутентификатор");
            }
            return(View(model));
        }
        public async Task <IActionResult> Login(AccountLoginModelView model)
        {
            if (ModelState.IsValid)
            {
                Employee employee = await _db.Employees.
                                    FirstOrDefaultAsync(p => p.Email == model.Authentificator ||
                                                        p.UserName == model.Authentificator);

                /*if(employee.OnTimePassword && await _userManager.CheckPasswordAsync(employee ,model.Password))
                 * {
                 *  if (employee.PasswordState == PasswordStates.DisposableUsed)
                 *  {
                 *      ModelState.AddModelError("", "Одноразовый пароль уже был использован для входа");
                 *      return View(model);
                 *  }
                 *  else
                 *  {
                 *      employee.PasswordState = PasswordStates.DisposableUsed;
                 *      _db.Entry(employee).State = EntityState.Modified;
                 *      await _db.SaveChangesAsync();
                 *  }
                 * }*/
                Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync(
                    employee,
                    model.Password,
                    true,
                    false
                    );

                if (result.Succeeded)
                {
                    if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }
                    var roles = _userManager.GetRolesAsync(employee);
                    foreach (var role in roles.Result)
                    {
                        if (role == "manager")
                        {
                            return(RedirectToAction("Index", "Manager"));
                        }
                        else if (role == "chief")
                        {
                            return(RedirectToAction("Index", "Chief"));
                        }
                        else if (role == "admin")
                        {
                            return(RedirectToAction("Index", "Admin"));
                        }
                        else if (role == "seller")
                        {
                            return(RedirectToAction("Index", "Seller"));
                        }
                        else if (role == "marketer")
                        {
                            return(RedirectToAction("Details", "Marketer"));
                        }
                    }
                    return(RedirectToAction("Index", "Employees"));
                }
                ModelState.AddModelError("", "Не корректный пароль и(или) аутентификатор");
            }
            return(View(model));
        }