Created
March 31, 2019 12:39
-
-
Save Mirch/201202a73cf857cacea72b153b97cd27 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
public class TokenService : ITokenService | |
{ | |
private readonly AuthenticationSettings _authenticationSettings; | |
public TokenService(IOptions<AuthenticationSettings> authenticationSettings) | |
{ | |
_authenticationSettings = authenticationSettings.Value; | |
} | |
public string BuildToken(string email, string role = "User") | |
{ | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
var key = Encoding.UTF8.GetBytes(_authenticationSettings.JwtKey); | |
var tokenDescriptor = new SecurityTokenDescriptor | |
{ | |
Subject = new ClaimsIdentity(new Claim[] | |
{ | |
new Claim(ClaimTypes.Name, email), | |
new Claim(ClaimTypes.Role, role) | |
}), | |
Expires = DateTime.UtcNow.AddDays(7), | |
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) | |
}; | |
var jwtToken = tokenHandler.CreateToken(tokenDescriptor); | |
var token = tokenHandler.WriteToken(jwtToken); | |
return token; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment