public IActionResult Login([FromBody] Login login)
        {
            IEnumerable <User> users = _dataRepository.GetAll();
            User user = users.FirstOrDefault(x => x.UserName == login.UserName && x.Password == login.Password);

            if (user != null)
            {
                string accessToken = JWTTokenCreator.GetToken(GetClaims(user), _configuration);
                return(Ok(new {
                    accessToken,
                    user.Role
                }));
            }
            return(Unauthorized());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel loginViewModel)
        {
            var user = await _userManager.FindByEmailAsync(loginViewModel.Email);

            if (user == null)
            {
                return(BadRequest("Invalid login"));
            }
            //if (!user.EmailConfirmed)
            //{
            //    return BadRequest("Confirm your email first");
            //}

            var passwordSignInResult = await _signInManager.PasswordSignInAsync(user, loginViewModel.Password, isPersistent : loginViewModel.RememberMe, lockoutOnFailure : false);

            if (!passwordSignInResult.Succeeded)
            {
                return(BadRequest("Invalid login"));
            }

            var claims = await _userManager.GetClaimsAsync(user);

            return(Ok(new { token = JWTTokenCreator.GetToken(loginViewModel, claims), claims = claims, uid = user.Id }));
        }