ApiTokenAttribute.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Microsoft.AspNetCore.Mvc.Filters;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using TEAMModelOS.Models;
  6. using TEAMModelOS.SDK.DI;
  7. namespace TEAMModelBI.Filter
  8. {
  9. public class ApiTokenAttribute : Attribute, IFilterFactory
  10. {
  11. public bool IsReusable => throw new NotImplementedException();
  12. /// <summary>
  13. /// 是否开启限流策略
  14. /// </summary>
  15. public bool Limit { get; set; }
  16. /// <summary>
  17. /// 授权序列
  18. /// </summary>
  19. public string Auth { get; set; }
  20. public IFilterMetadata CreateInstance(IServiceProvider services)
  21. {
  22. var option = services.GetService<IOptions<Option>>();
  23. var azureRedis = services.GetService<AzureRedisFactory>();
  24. return new InternalAuthTokenFilter(option, azureRedis, Auth, Limit);
  25. }
  26. private class InternalAuthTokenFilter : IResourceFilter
  27. {
  28. private readonly Option _option;
  29. //private readonly string _roles;
  30. private readonly string _auth;
  31. private readonly bool _limit;
  32. private readonly AzureRedisFactory _azureRedis;
  33. public InternalAuthTokenFilter(IOptions<Option> option, AzureRedisFactory azureRedis, string auth, bool limit)
  34. {
  35. _option = option.Value;
  36. _auth = auth;
  37. _limit = limit;
  38. _azureRedis = azureRedis;
  39. }
  40. public void OnResourceExecuting(ResourceExecutingContext context)
  41. {
  42. }
  43. public void OnResourceExecuted(ResourceExecutedContext context)
  44. {
  45. }
  46. }
  47. }
  48. }