OASystemAuthentication.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Microsoft.AspNetCore.Mvc.Filters;
  2. using System.IdentityModel.Tokens.Jwt;
  3. namespace OASystem.API
  4. {
  5. public class OASystemAuthentication : AuthorizeAttribute
  6. {
  7. public void OnAuthorization(AuthorizationFilterContext context)
  8. {
  9. string id = context.HttpContext.User.FindFirst("id")?.Value;
  10. if (string.IsNullOrEmpty(id))
  11. {
  12. context.Result = new StatusCodeResult(401); //返回鉴权失败
  13. return;
  14. }
  15. Console.WriteLine("我是Authorization过滤器");
  16. // 请求的地址
  17. //var url = context.HttpContext.Request.Path.Value;
  18. // 请求头信息
  19. var heads = context.HttpContext.Request.Headers;
  20. string token = heads["Authorization"];
  21. token = token.Replace("Bearer ", "");//去掉 "Bearer "才是真正的token
  22. if (string.IsNullOrEmpty(token))
  23. {
  24. Console.WriteLine("校验不通过");
  25. return;
  26. }
  27. //redis校验这个token的有效性,确定来源是sso和确定会话没过期
  28. //解析员工userNumber
  29. var jwtHandler = new JwtSecurityTokenHandler();
  30. JwtSecurityToken securityToken = jwtHandler.ReadJwtToken(token);
  31. //DateTime expDt = (securityToken.Payload[JwtRegisteredClaimNames.c] ?? 0).GetInt().GetTimeSpmpToDate();
  32. //if (!_cacheService.StringGet<bool>($"token:{token}"))
  33. //{
  34. // Console.WriteLine($"token无效,token:{token}");
  35. // context.Result = new StatusCodeResult(401); //返回鉴权失败
  36. // return;
  37. //}
  38. }
  39. }
  40. }