roadie/Roadie.Api.Services/TokenService.cs
2019-06-30 17:14:36 -05:00

60 lines
No EOL
2.3 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Roadie.Library.Identity;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace Roadie.Api.Services
{
public class TokenService : ITokenService
{
private readonly IConfiguration _configuration;
public TokenService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<string> GenerateToken(ApplicationUser user, UserManager<ApplicationUser> userManager)
{
var utcNow = DateTime.UtcNow;
var roles = await userManager.GetRolesAsync(user);
var userRoles = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToArray();
var tokenHandler = new JwtSecurityTokenHandler();
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new Claim("roadie_id", user.RoadieId.ToString()),
new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName),
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, utcNow.ToString())
}.Union(userRoles);
var now = DateTime.UtcNow;
var securityKey =
new SymmetricSecurityKey(
Encoding.Default.GetBytes(_configuration.GetValue<string>("Tokens:PrivateKey")));
var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
var jwt = new JwtSecurityToken(
signingCredentials: signingCredentials,
claims: claims,
notBefore: utcNow,
expires: utcNow.AddSeconds(_configuration.GetValue<int>("Tokens:Lifetime")),
audience: _configuration.GetValue<string>("Tokens:Audience"),
issuer: _configuration.GetValue<string>("Tokens:Issuer")
);
return new JwtSecurityTokenHandler().WriteToken(jwt);
}
}
}