我最近从 Swashbuckle 过渡到 Swagger-Net .进行更改后我遇到的一个问题是,现在我无法调用需要在 Authorization header 中发送 token 的 API。下面是我在 SwaggerConfig.cs 中的代码之前在 Swashbuckle 和现在 Swagger-Net 中的方式
Swashbuckle
//section for .EnableSwagger
c.ApiKey("apiKey")
.Description("API Key Authentication")
.Name("Authorization")
.In("header");
//section for .EnableSwaggerUI
c.EnableApiKeySupport("Authorization", "header");
Swagger-Net
//section for .EnableSwagger
c.ApiKey("Authorization", "header", "API Key Authentication");
对于 Swagger-Net,我在 .EnableSwaggerUI 部分找不到任何等效的 .EnableAPIKeySupport 。在访问/Swagger UI 渲染并使用 Authorize 传递我的 token 后,它不会将该 token 发送到 API。我可以说它没有被发送,因为它不在给定的示例 CURL 中。
最佳答案
是在 Swagger-Net ApiKey
是你所需要的全部
c.ApiKey("apiKey", "header", "API Key Authentication", typeof(KeyAuthAttribute));
这是一个工作示例:
http://turoapi.azurewebsites.net/swagger/ui/index#/Echo/Echo_Post
“ protected ”操作将在右侧显示锁定图标
当你执行它们时,你可以看到 curl 有正确的东西
后面的代码在这里:
https://github.com/heldersepu/TuroApi/blob/master/TuroApi/App_Start/SwaggerConfig.cs#L67
关于swagger - 使用 Swagger-Net 在 Swagger 中发送不记名 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433796/