Skip to content

Instantly share code, notes, and snippets.

@DeebiKaaRaviSankar
Created August 19, 2022 05:36
Show Gist options
  • Save DeebiKaaRaviSankar/7e3d8f49e612ec41e89466e57a9b6438 to your computer and use it in GitHub Desktop.
Save DeebiKaaRaviSankar/7e3d8f49e612ec41e89466e57a9b6438 to your computer and use it in GitHub Desktop.
namespace GlintApi.Services
{
public class ControllerService : IControllerService
{
private IConfiguration _configuration;
public ControllerService(IConfiguration configuration)
{
_configuration = configuration;
}
public string CreateToken(User user)
{
var claims = new[] {
new Claim(JwtRegisteredClaimNames.Sub, _configuration[DbConstants.ValidSubject]),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()),
new Claim(DbConstants.Claims_UserID, user.Id.ToString()),
new Claim(DbConstants.Claims_UserName, user.UserName),
new Claim(DbConstants.Claims_Email, user.Email),
new Claim(ClaimTypes.Role, user.AuthRole)
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration[DbConstants.IssuerSigningKey]));
var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
_configuration[DbConstants.ValidIssuer],
_configuration[DbConstants.ValidAudience],
claims,
expires: DateTime.UtcNow.AddDays(1),
signingCredentials: signIn
);
return new JwtSecurityTokenHandler().WriteToken(token);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment