StrandardApiAuth.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  2. using Microsoft.OpenApi.Models;
  3. using Swashbuckle.AspNetCore.SwaggerGen;
  4. using System;
  5. using System.Reflection;
  6. using System.Linq;
  7. using Microsoft.Extensions.DependencyInjection;
  8. namespace XYY.Core.SwaggerGen.Strandard
  9. {
  10. public static class StrandardApiAuth
  11. {
  12. public static void SetAuth(this SwaggerGenOptions c)
  13. {
  14. var apiSS = new OpenApiSecurityScheme()
  15. {
  16. Description = "请求头必须添加token,传入token: \"Authorization: token {token}\"",
  17. Name = "Authorization",
  18. //参数添加在头部
  19. In = ParameterLocation.Header,
  20. //使用Authorize头部
  21. Type = SecuritySchemeType.Http,
  22. //内容为以 bearer开头
  23. Scheme = "bearer",
  24. };
  25. var securityRequirement = new OpenApiSecurityRequirement
  26. {
  27. {
  28. new OpenApiSecurityScheme
  29. {
  30. Reference = new OpenApiReference
  31. {
  32. Type = ReferenceType.SecurityScheme,
  33. Id = "bearerAuth",
  34. }
  35. },
  36. new string[] {}
  37. }
  38. };
  39. c.AddSecurityDefinition("bearerAuth", apiSS);
  40. c.AddSecurityRequirement(securityRequirement);
  41. }
  42. }
  43. }