birbmap/Birdmap/Controllers/AuthController.cs

62 lines
2.2 KiB
C#
Raw Normal View History

2020-10-21 17:04:54 +02:00
using Birdmap.Models;
using Birdmap.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace Birdmap.Controllers
{
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IAuthService _service;
private readonly IConfiguration _configuration;
public AuthController(IAuthService service, IConfiguration configuration)
{
_service = service;
_configuration = configuration;
}
[AllowAnonymous]
[HttpPost("authenticate")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest model)
{
var user = await _service.AuthenticateUserAsync(model.Username, model.Password);
2020-10-23 15:05:20 +02:00
var expiresInSeconds = TimeSpan.FromHours(2).TotalSeconds;
2020-10-21 17:04:54 +02:00
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_configuration["BasicAuth:Secret"]);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, user.Name)
}),
2020-10-23 15:05:20 +02:00
Expires = DateTime.UtcNow.AddHours(expiresInSeconds),
2020-10-21 17:04:54 +02:00
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
var tokenString = tokenHandler.WriteToken(token);
return Ok(
new
{
2020-10-23 15:05:20 +02:00
user_name = user.Name,
access_token = tokenString,
token_type = "Bearer",
expires_in = expiresInSeconds,
2020-10-21 17:04:54 +02:00
});
}
}
}