You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.5 KiB
45 lines
1.5 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
|
|
namespace API.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public AuthController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("token")]
|
|
public IActionResult GetToken()
|
|
{
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_configuration["JwtTokenSettings:SymmetricSecurityKey"]);
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(new Claim[]
|
|
{
|
|
new Claim(ClaimTypes.Name, "User1"),
|
|
}),
|
|
Expires = DateTime.UtcNow.AddHours(1),
|
|
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
|
|
Audience = _configuration["JwtTokenSettings:ValidAudience"],
|
|
Issuer = _configuration["JwtTokenSettings:ValidIssuer"]
|
|
};
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
var tokenString = tokenHandler.WriteToken(token);
|
|
|
|
return Ok(new { Token = tokenString });
|
|
}
|
|
|
|
}
|
|
}
|