예제 #1
0
        public async Task <IActionResult> Login(UserRegisterLoginDto userLoginInfo)
        {
            bool   result;
            string message;
            string jwt = null;

            try
            {
                result = await _authenticationService.Login(userLoginInfo.Username, userLoginInfo.Password);

                if (result)
                {
                    message = "Successfully logged in.";
                    jwt     = GenerateToken(userLoginInfo);
                }
                else
                {
                    message = "Invalid username/password combination.";
                }
            }
            catch (Exception e)
            {
                return(Problem(e.Message));
            }

            return(Ok(new { Status = result, Message = message, Token = jwt }));
        }
예제 #2
0
        private string GenerateToken(UserRegisterLoginDto authenticatedUser)
        {
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:Secret").Value);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, authenticatedUser.Username),
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature)
            };
            var createdToken = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(createdToken));
        }
예제 #3
0
        public async Task <IActionResult> Test(UserRegisterLoginDto user)
        {
            using (var session = _cassandraDbConnectionProvider.Connect())
            {
                try
                {
                    var result = await _authenticationService.Register(user.Username, user.Password);

                    return(Ok(result));
                }
                catch (Exception)
                {
                    return(BadRequest());
                }
            }

            return(NoContent());
        }