Exemplo n.º 1
0
        public ActionResult Login(LoginDto user)
        {
            if (ModelState.IsValid)
            {
                var loginResult = _userService.TryLogin(user.Email, user.Password);

                if (loginResult)
                {
                    _cookie.SetCookie(user.Email, user.RememberMe);
                    var actualUser = _userRepo.GetByEmail(user.Email);
                    var claims     = new List <Claim>()
                    {
                        new Claim(ClaimTypes.NameIdentifier, actualUser.ToString()),
                        new Claim(ClaimTypes.Name, actualUser.Name),
                        new Claim(ClaimTypes.Surname, actualUser.Surname),
                        new Claim(ClaimTypes.Email, actualUser.Email)
                    };

                    Authentication.SignIn(
                        new AuthenticationProperties
                    {
                        AllowRefresh = true,
                        IsPersistent = false
                    },
                        new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie));

                    return(RedirectToAction("UserPage", user));
                }
                else
                {
                    ModelState.AddModelError("incorrect login", "Login data is incorrect!");
                }
            }
            return(View(user));
            //return false;
        }