Example #1
0
        public async Task <ActionResult <AuthenticationResponse> > Authenticate()
        {
            if (User.IsAuthenticated())
            {
                return(Ok(new AuthenticationResponse
                {
                    IsAuthenticated = true
                }));
            }

            string userId =
                await AddUserIdClaimIfEmpty() ??
                User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            ApplicationUser user = await _userStore.FindByIdAsync(userId, CancellationToken.None);

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

            var authToken    = _tokenHelper.GenerateJwtToken(user);
            var refreshToken = await _tokenHelper.GenerateRefreshToken(user);

            return(Ok(new AuthenticationResponse
            {
                AuthToken = authToken,
                RefreshToken = refreshToken
            }));
        }
Example #2
0
        public IActionResult Authenticate([FromBody] LoginVM loginVm)
        {
            using (TodoContext _context = new TodoContext())
            {
                if (loginVm == null || string.IsNullOrEmpty(loginVm.Username) || string.IsNullOrEmpty(loginVm.Password))
                {
                    return(BadRequest());
                }

                User user = _context.Users.SingleOrDefault(u => u.Username == loginVm.Username && u.Password == loginVm.Password);
                if (user == null)
                {
                    return(Unauthorized());
                }

                string token = _tokenHelper.GenerateJwtToken(user);

                return(Ok(JsonConvert.SerializeObject(token)));
            }
        }