Ejemplo n.º 1
0
        public async Task <IActionResult> Authenticate(LoginViewModel loginViewModel)
        {
            try
            {
                var user = await _userTaskService.GetUserByCredentials(loginViewModel.UserName, loginViewModel.Password);

                if (user == null)
                {
                    return(BadRequest());
                }

                //  todo: maybe move this lot to a service
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
                };

                var userIdentity = new ClaimsIdentity(claims, "login");

                ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);

                await HttpContext.SignInAsync(principal);

                return(Ok());
            }
            catch (Exception)
            {
                //  todo: return a user-friendly error message
                return(BadRequest());
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> LogIn(LoginInput input)
        {
            if (!ModelState.IsValid)
            {
                return(View(input));
            }

            try
            {
                var user = await _userTaskService.GetUserByCredentials(input.Email, input.Password);

                if (user == null)
                {
                    return(BadRequest());
                }

                Auth.Authenticate(user, HttpContext.Response);

                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }