JwtAuthExtension.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Microsoft.AspNetCore.Authentication.JwtBearer;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.IdentityModel.Tokens;
  6. using System;
  7. using System.Threading.Tasks;
  8. using System.Security.Claims;
  9. using System.IdentityModel.Tokens.Jwt;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. namespace TEAMModelOS.SDK.Extension
  13. {
  14. public static class JwtAuthExtension
  15. {
  16. public static string CreateAuthToken(string issuer, string id,string name,string picture, string salt, string schoolID = "", string[] roles = null, string[] permissions = null, int expire = 1)
  17. {
  18. // 設定要加入到 JWT Token 中的聲明資訊(Claims)
  19. var payload = new JwtPayload {
  20. { JwtRegisteredClaimNames.Iss, issuer }, //發行者
  21. { JwtRegisteredClaimNames.Sub, id }, // 用戶ID
  22. { JwtRegisteredClaimNames.Azp,schoolID}, // 學校簡碼,如果有的話
  23. { JwtRegisteredClaimNames.Exp,DateTimeOffset.UtcNow.AddHours(expire).ToUnixTimeSeconds().ToString()}, // 到期的時間,必須為數字
  24. { "name",name}, // 用戶的顯示名稱
  25. { "picture",picture}, // 用戶頭像
  26. { "roles",roles}, // 登入者的角色,角色類型 (Admin、Teacher、Student)
  27. { "permissions",permissions} //登入者的權限請求
  28. };
  29. // 建立一組對稱式加密的金鑰,主要用於 JWT 簽章之用
  30. var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(salt));
  31. // HmacSha256 有要求必須要大於 128 bits,所以 salt 不能太短,至少要 16 字元以上
  32. // https://stackoverflow.com/questions/47279947/idx10603-the-algorithm-hs256-requires-the-securitykey-keysize-to-be-greater
  33. var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
  34. var header = new JwtHeader(signingCredentials);
  35. var secToken = new JwtSecurityToken(header, payload);
  36. // 產出所需要的 JWT securityToken 物件,並取得序列化後的 Token 結果(字串格式)
  37. var tokenHandler = new JwtSecurityTokenHandler();
  38. //var securityToken = tokenHandler.CreateToken(tokenDescriptor);
  39. var serializeToken = tokenHandler.WriteToken(secToken);
  40. return serializeToken;
  41. }
  42. public static string CreateAppToken(string issuer, string id, string salt, string schoolID = "", int expire = 1)
  43. {
  44. // 設定要加入到 JWT Token 中的聲明資訊(Claims)
  45. var payload = new JwtPayload {
  46. { JwtRegisteredClaimNames.Iss, issuer }, //發行者
  47. { JwtRegisteredClaimNames.Sub, id }, // 用戶ID
  48. { JwtRegisteredClaimNames.Azp,schoolID}, // 學校簡碼,如果有的話
  49. { JwtRegisteredClaimNames.Exp,DateTimeOffset.UtcNow.AddHours(expire).ToUnixTimeSeconds().ToString()}, // 到期的時間,必須為數字
  50. //{ "name",name}, // 用戶的顯示名稱
  51. //{ "picture",picture}, // 用戶頭像
  52. //{ "roles",roles}, // 登入者的角色,角色類型 (Admin、Teacher、Student)
  53. // { "permissions",permissions} //登入者的權限請求
  54. };
  55. // 建立一組對稱式加密的金鑰,主要用於 JWT 簽章之用
  56. var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(salt));
  57. // HmacSha256 有要求必須要大於 128 bits,所以 salt 不能太短,至少要 16 字元以上
  58. // https://stackoverflow.com/questions/47279947/idx10603-the-algorithm-hs256-requires-the-securitykey-keysize-to-be-greater
  59. var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
  60. var header = new JwtHeader(signingCredentials);
  61. var secToken = new JwtSecurityToken(header, payload);
  62. // 產出所需要的 JWT securityToken 物件,並取得序列化後的 Token 結果(字串格式)
  63. var tokenHandler = new JwtSecurityTokenHandler();
  64. //var securityToken = tokenHandler.CreateToken(tokenDescriptor);
  65. var serializeToken = tokenHandler.WriteToken(secToken);
  66. return serializeToken;
  67. }
  68. public static bool ValidateAuthToken(string token, string salt)
  69. {
  70. try
  71. {
  72. var handler = new JwtSecurityTokenHandler();
  73. var validationParameters = new TokenValidationParameters
  74. {
  75. RequireExpirationTime = true,
  76. ValidateIssuer = false,
  77. ValidateAudience = false,
  78. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(salt)),
  79. ValidateLifetime = false,
  80. //LifetimeValidator = LifetimeValidator,
  81. ClockSkew = TimeSpan.Zero
  82. };
  83. ClaimsPrincipal principal = handler.ValidateToken(token, validationParameters, out SecurityToken securityToken);
  84. return true;
  85. }
  86. catch (Exception)
  87. {
  88. //Trace.WriteLine(ex.Message);
  89. return false;
  90. }
  91. }
  92. }
  93. }