예제 #1
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));
        }
        public IActionResult SignIn(AccountDto accountDto)
        {
            try
            {
                //TODO: сделать проверку через посторонний сервис для валидации данных а потом уже либо на главную страницу либо сообщение и вернуть на страницу входа
                var account = new Account()
                {
                    Login = accountDto.Login
                };

                var(message, success) = _account.Login(accountDto);
                return(View("Home/Index"));
            }
            catch
            {
                return(View());
            }
        }