使用 Swagger-Net 在 Swagger 中发送不记名令牌
Send bearer token in Swagger using Swagger-Net
我最近从 Swashbuckle 过渡到 Swagger-Net。进行更改后我遇到的一个问题是,现在我无法调用我的 API,这需要在授权 header 中发送令牌。以下是我之前在 Swashbuckle 和现在 Swagger-Net
中的 SwaggerConfig.cs 中的代码
虚张声势
//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 传递我的令牌后,它不会将该令牌发送到 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
我最近从 Swashbuckle 过渡到 Swagger-Net。进行更改后我遇到的一个问题是,现在我无法调用我的 API,这需要在授权 header 中发送令牌。以下是我之前在 Swashbuckle 和现在 Swagger-Net
中的 SwaggerConfig.cs 中的代码虚张声势
//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 传递我的令牌后,它不会将该令牌发送到 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