I have an existing C# ASP.NET Web API 2 project (.NET 4.6.1) where I need to integrate Swagger to generate the documentation as well as the client SDKs (only C# for the moment). This as been done using Swashbuckle in its latest version (5.5.3).
Everything went well except one thing. The issue I'm having is that the security (apiKey through HTTP headers) defined in my SwaggerConfig.cs ends up in the output JSON but somehow it's not linked to any of the methods (even though it is mandatory).
My security config is defined as follow:
GlobalConfiguration.Configuration.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "Dummy API")
c.ApiKey("apiKey")
.Description("API Key Authentication")
.Name("X-API-Key")
.In("header");
}).EnableSwaggerUi(c =>
{
c.EnableApiKeySupport("X-API-Key", "header");
});
and the result in the generated Swagger JSON:
"securityDefinitions": {
"apiKey": {
"type": "apiKey",
"description": "API Key Authentication",
"name": "X-API-Key",
"in": "header"
}
}
And here is what I obtain:
"/api/ping": {
"get": {
"tags": [
"Dummy"
],
"summary": "Ping.",
"operationId": "ping",
"consumes": [],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
compared to what I want to obtain:
"/api/ping": {
"get": {
"tags": [
"Dummy"
],
"summary": "Ping.",
"operationId": "ping",
"consumes": [],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
},
"security": [
{
"apiKey": []
}
]
}
}
Any idea what I should change in the project so the security part is generated?