123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System;
- using System.Configuration;
- using System.IdentityModel.Tokens;
- using System.Security.Claims;
-
- 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 = ConfigurationManager.AppSettings["CredentialKey"];
- 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="username">The user username</param>
- /// <param name="salt">The user salt</param>
- /// <returns>The token</returns>
- public static string GetToken(string username, string salt)
- {
- var stringValidator = ConfigurationManager.AppSettings["StringValidator"];
- JwtSecurityToken jst = new JwtSecurityToken("urn:" + stringValidator,
- stringValidator,
- new []
- {
- new Claim(ClaimTypes.Name, username),
- new Claim(ClaimTypes.Authentication, salt)
- }, null, DateTime.Now.AddDays(1),
- CreateSigningCredentials());
-
- JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
- return jh.WriteToken(jst);
- }
- }
- }
|