Jeff Fol
Jeff Fol

Reputation: 1410

Send bearer token in Swagger using Swagger-Net

I have recently made the transition from Swashbuckle to Swagger-Net. One problem that I'm having after making the change is that now I'm unable to call my APIs which require a token sent in the Authorization header. Below are how I had the code in SwaggerConfig.cs before in Swashbuckle and now 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");

For Swagger-Net I can't find any equivalent of the .EnableAPIKeySupport in the .EnableSwaggerUI portion. After accessing the /Swagger UI rendering and using Authorize passing my token it is not sending that token to the API. I can tell it's not being sent as it is not in the sample CURL given.

Upvotes: 4

Views: 2647

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17574

Yes on Swagger-Net the ApiKey is all you need

c.ApiKey("apiKey", "header", "API Key Authentication", typeof(KeyAuthAttribute));

Here is a working example:
http://turoapi.azurewebsites.net/swagger/ui/index#/Echo/Echo_Post

The "protected" actions will show a lock icon on the right enter image description here

And when you execute them you can see that the curl has the right stuff enter image description here

And the code behind is here:
https://github.com/heldersepu/TuroApi/blob/master/TuroApi/App_Start/SwaggerConfig.cs#L67

Upvotes: 4

Related Questions