コード例 #1
0
        public IActionResult Authenticate([FromBody] CommandAuthenticateDto commandAuthenticateDto)
        {
            var user = _repository.Authenticate(commandAuthenticateDto.Line, commandAuthenticateDto.Platform);

            if (user == null)
            {
                return(BadRequest(new { message = "Either Username or password is incorrect" }));
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes("This is my authenticating secret Key");
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(10),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            return(Ok(new
            {
                Id = user.Id,
                Line = user.Line,
                TokenContext = tokenString
            }));
        }
コード例 #2
0
        public IActionResult Authenticate([FromBody] AuthenticateModel model)
        {
            var user = _repository.Authenticate(model.Username, model.Password);

            if (user == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            return(Ok(user));
        }