You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.2 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IdentityModel.Tokens.Jwt;
  5. using System.Linq;
  6. using System.Security.Claims;
  7. using System.Web;
  8. namespace JsonRx.Helper
  9. {
  10. /// <summary>
  11. /// </summary>
  12. public class JwtTokenizer
  13. {
  14. private string username;
  15. private string guid;
  16. private string customerId;
  17. private string fcmId;
  18. private string deviceType;
  19. /// <summary>
  20. /// </summary>
  21. public JwtTokenizer(string username, string guid, string customerId, string FCMId, string deviceTpe="")
  22. {
  23. this.username = username;
  24. this.guid = guid;
  25. this.customerId = customerId;
  26. this.fcmId = FCMId;
  27. this.deviceType = deviceTpe;
  28. }
  29. /// <summary>
  30. /// </summary>
  31. /// <returns></returns>
  32. public string CreateToken()
  33. {
  34. //Set issued at date
  35. DateTime issuedAt = DateTime.UtcNow;
  36. //set the time when it expires
  37. var time = ConfigurationManager.AppSettings["TokenExpiryInMinute"].ToString();
  38. DateTime expires = DateTime.UtcNow.AddMinutes(Convert.ToDouble(time));
  39. //http://stackoverflow.com/questions/18223868/how-to-encrypt-jwt-security-token
  40. var tokenHandler = new JwtSecurityTokenHandler();
  41. IList<Claim> claims = new List<Claim>();
  42. if (!string.IsNullOrEmpty(username))
  43. {
  44. claims.Add(new Claim(ClaimTypes.Name, username));
  45. }
  46. if (!string.IsNullOrEmpty(guid))
  47. {
  48. claims.Add(new Claim("Guid", guid));
  49. }
  50. if (!string.IsNullOrEmpty(customerId))
  51. {
  52. claims.Add(new Claim("CustomerNo", customerId));
  53. }
  54. if (!string.IsNullOrEmpty(fcmId))
  55. {
  56. claims.Add(new Claim("ClientFcmId", fcmId));
  57. }
  58. if (!string.IsNullOrEmpty(deviceType))
  59. {
  60. claims.Add(new Claim("DeviceType", deviceType));
  61. }
  62. //create a identity and add claims to the user which we want to log in
  63. ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims);
  64. string sec = ConfigurationManager.AppSettings["JwtSecret"].ToString();
  65. //var now = DateTime.UtcNow;
  66. var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(sec));
  67. var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256Signature);
  68. //create the jwt
  69. var token =
  70. (JwtSecurityToken)
  71. tokenHandler.CreateJwtSecurityToken(issuer: ConfigurationManager.AppSettings["JwtAudience"].ToString(), audience: ConfigurationManager.AppSettings["JwtIssuer"].ToString(),
  72. subject: claimsIdentity, notBefore: issuedAt, expires: expires, signingCredentials: signingCredentials);
  73. var tokenString = tokenHandler.WriteToken(token);
  74. return tokenString;
  75. }
  76. }
  77. }