1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using System.IdentityModel.Tokens;
- using System.Security.Claims;
- using iiie.Authentication.DBO;
-
- namespace iiie.Authentication.Business.JWT
- {
- /// <summary>
- /// Token creation class
- /// </summary>
- public static class TokenManager
- {
- /// <summary>
- /// Create signing credentials to sign the token
- /// </summary>
- /// <returns>The credentials</returns>
- private static SigningCredentials CreateSigningCredentials()
- {
- string symmetricKey = AuthProvider.Instance.GetCredentialKey();
- byte[] keybytes = Convert.FromBase64String(symmetricKey);
- SecurityKey securityKey = new InMemorySymmetricSecurityKey(keybytes);
- SigningCredentials signingCredentials =
- new SigningCredentials(securityKey,
- SecurityAlgorithms.HmacSha256Signature,
- SecurityAlgorithms.Sha256Digest);
- return signingCredentials;
- }
-
- /// <summary>
- /// Create a JWT token
- /// </summary>
- /// <param name="user">The user</param>
- /// <returns>The token</returns>
- public static string GetToken(UserDboAuth user, String salt)
- {
- var stringValidator = AuthProvider.Instance.GetValidatorString();
- JwtSecurityToken jst = new JwtSecurityToken("urn:" + stringValidator,
- stringValidator,
- new Claim[]
- {
- new Claim(ClaimTypes.Name, user.Username),
- new Claim(ClaimTypes.Authentication, salt)
- }, null, DateTime.Now.AddDays(1),
- CreateSigningCredentials());
-
- JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
- return jh.WriteToken(jst);
- }
- }
- }
|