Example #1
0
        public IActionResult GetToken(string username, string password)
        {
            ClaimsIdentity claimsIdentity = GetPerson(username, password);

            if (claimsIdentity != null)
            {
                DateTime         currentTime      = DateTime.Now;
                JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(issuer: AuthenticationToken.Issuer,
                                                                         AuthenticationToken.Audience,
                                                                         claimsIdentity.Claims,
                                                                         currentTime,
                                                                         currentTime.Add(TimeSpan.FromMinutes(AuthenticationToken.TTL)),
                                                                         new SigningCredentials(AuthenticationToken.GetSymmetricSecurityKey(),
                                                                                                SecurityAlgorithms.HmacSha256));
                string token =
                    new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
                var response = new
                {
                    access_token = token,
                    username     = claimsIdentity.Name
                };
                return(Json(response));
            }
            else
            {
                return(BadRequest(400));
            }
        }