-
-
Save DeebiKaaRaviSankar/7e3d8f49e612ec41e89466e57a9b6438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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