IJwtSecurity.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Security.Authentication;
  6. using System.Security.Claims;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Options;
  12. using Microsoft.IdentityModel.Tokens;
  13. using XF.Domain.Dependency;
  14. using XF.Domain.Exceptions;
  15. using XF.Domain.Options;
  16. namespace XF.Domain.Authentications
  17. {
  18. public interface IJwtSecurity
  19. {
  20. string EncodeJwtToken(ICollection<Claim> claims);
  21. void DecodeJwtToken(string jwt);
  22. }
  23. public class JwtSecurity : IJwtSecurity, IScopeDependency
  24. {
  25. private readonly IOptions<IdentityConfiguration> _jwtConfigOptionAccessor;
  26. private readonly IHttpContextAccessor _contextAccessor;
  27. public JwtSecurity(IOptions<IdentityConfiguration> jwtConfigOptionAccessor, IHttpContextAccessor contextAccessor)
  28. {
  29. _jwtConfigOptionAccessor = jwtConfigOptionAccessor;
  30. _contextAccessor = contextAccessor;
  31. }
  32. public string EncodeJwtToken(ICollection<Claim> claims)
  33. {
  34. var jwtOptions = _jwtConfigOptionAccessor.Value.Jwt;
  35. if (jwtOptions == null)
  36. throw new ArgumentNullException(nameof(jwtOptions));
  37. var bytes = Encoding.UTF8.GetBytes(jwtOptions.SecretKey);
  38. var securityKey = new SymmetricSecurityKey(bytes);
  39. var signingCredentials =
  40. new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
  41. var expired = DateTime.Now.AddMinutes(jwtOptions.Expired);
  42. var jwtSecurityToken = new JwtSecurityToken(jwtOptions.Issuer, jwtOptions.Audience, claims, DateTime.Now, expired,
  43. signingCredentials);
  44. var tokenHandler = new JwtSecurityTokenHandler();
  45. var token = tokenHandler.WriteToken(jwtSecurityToken);
  46. return token;
  47. }
  48. public void DecodeJwtToken(string jwt)
  49. {
  50. var jwtOptions = _jwtConfigOptionAccessor.Value.Jwt;
  51. if (jwtOptions == null)
  52. throw new ArgumentNullException(nameof(jwtOptions));
  53. JwtSecurityTokenHandler tokenHandler = new();
  54. TokenValidationParameters valParam = new();
  55. var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecretKey));
  56. valParam.IssuerSigningKey = securityKey;
  57. valParam.ValidateIssuer = false;
  58. valParam.ValidateAudience = false;
  59. ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(jwt, valParam, out SecurityToken secToken);
  60. //foreach (var claim in claimsPrincipal.Claims)
  61. //{
  62. // Console.WriteLine($"{claim.Type}={claim.Value}");
  63. //}
  64. if (_contextAccessor.HttpContext is null)
  65. throw new AuthenticationException($"{nameof(_contextAccessor.HttpContext)} is null");
  66. _contextAccessor.HttpContext.User = claimsPrincipal;
  67. }
  68. }
  69. }