Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JwtManager.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using BlackRock.Reporting.API.Core.Models;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.IdentityModel.Tokens;
  4. using System.IdentityModel.Tokens.Jwt;
  5. using System.Security.Claims;
  6. namespace BlackRock.Reporting.API.Authentication
  7. {
  8. public class JwtManager : IJwtManager
  9. {
  10. //private string Secret = "db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw==";
  11. //private readonly IConfiguration configuration;
  12. private readonly string Secret;
  13. //public JwtManager(IConfiguration configuration)
  14. //{
  15. // this.configuration = configuration;
  16. // Secret = configuration["SecurityKey"];
  17. //}
  18. public string GenerateToken(string username, int expireMinutes = 20)
  19. {
  20. expireMinutes = 1;
  21. var symmetricKey = Convert.FromBase64String(Secret);
  22. var tokenHandler = new JwtSecurityTokenHandler();
  23. var now = DateTime.UtcNow;
  24. var tokenDescriptor = new SecurityTokenDescriptor
  25. {
  26. Subject = new ClaimsIdentity(new[]
  27. {
  28. new Claim(ClaimTypes.Name, username)
  29. }),
  30. Expires = now.AddMinutes(Convert.ToInt32(expireMinutes)),
  31. SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature)
  32. };
  33. SecurityToken securityToken = tokenHandler.CreateToken(tokenDescriptor);
  34. var token = tokenHandler.WriteToken(securityToken);
  35. return token;
  36. }
  37. public string GetUserName(string token)
  38. {
  39. try
  40. {
  41. var tokenHandler = new JwtSecurityTokenHandler();
  42. var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;
  43. if (jwtToken == null)
  44. return null;
  45. var symmetricKey = Convert.FromBase64String(Secret);
  46. var validationParameters = new TokenValidationParameters()
  47. {
  48. ValidateIssuer = false,
  49. ValidateAudience = false,
  50. ValidateLifetime = false,
  51. RequireExpirationTime = false,
  52. IssuerSigningKey = new SymmetricSecurityKey(symmetricKey)
  53. };
  54. SecurityToken validatedToken = new JwtSecurityToken();
  55. var principal = tokenHandler.ValidateToken(token, validationParameters, out validatedToken);
  56. return principal.Identity.Name;
  57. }
  58. catch (Exception)
  59. {
  60. return null;
  61. }
  62. }
  63. public ClaimsPrincipal GetPrincipal(string token)
  64. {
  65. try
  66. {
  67. var tokenHandler = new JwtSecurityTokenHandler();
  68. var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;
  69. if (jwtToken == null)
  70. return null;
  71. var symmetricKey = Convert.FromBase64String(Secret);
  72. var validationParameters = new TokenValidationParameters()
  73. {
  74. RequireExpirationTime = true,
  75. ValidateIssuer = false,
  76. ValidateLifetime = true,
  77. ValidateAudience = false,
  78. IssuerSigningKey = new SymmetricSecurityKey(symmetricKey)
  79. };
  80. SecurityToken validatedToken = new JwtSecurityToken();
  81. var principal = tokenHandler.ValidateToken(token, validationParameters, out validatedToken);
  82. return principal;
  83. }
  84. catch (Exception)
  85. {
  86. return null;
  87. }
  88. }
  89. }
  90. }