question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UseSwaggerUi3 : SwaggerSecuritySchemeType.ApiKey in Header ignored

See original GitHub issue

Hello! In order to transmit header value to all my APIs, I used settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender with app.UseSwaggerUi.

 app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
            {
                settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("TEST_HEADER", new SwaggerSecurityScheme
                {
                    Type = SwaggerSecuritySchemeType.ApiKey,
                    Name = "TEST_HEADER",
                    In = SwaggerSecurityApiKeyLocation.Header,
                    Description = "TEST_HEADER"
                }));
            });

In that way, all works like a charm !

curl -X GET --header 'Accept: text/html' --header 'TEST_HEADER: testvalue' 'http://localhost:60047/api/v2/users'

actionContext.HttpContext.Request.Headers["TEST_HEADER"] = "testvalue"

Then I try to update to UseSwaggerUi3 (because it’s better)

app.UseSwaggerUi3(typeof(Startup).GetTypeInfo().Assembly, settings =>
            {
                settings.GeneratorSettings.DocumentProcessors.Add(new SecurityDefinitionAppender("TEST_HEADER", new SwaggerSecurityScheme
                {
                    Type = SwaggerSecuritySchemeType.ApiKey,
                    Name = "TEST_HEADER",
                    In = SwaggerSecurityApiKeyLocation.Header,
                    Description = "TEST_HEADER"
                }));
            });

But now, the header is not sent 😦

curl -X GET "http://localhost:60047/api/v2/users" -H "accept: application/json" actionContext.HttpContext.Request.Headers["TEST_HEADER"] = {}

I use : NSwag.AspNetCore Version=11.17.2

Thank you in advance, and if you have another method to send headers globally it could be great 😃

Rémi

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
diegomgarciacommented, Dec 25, 2018

I’ve just setup like that and it works fine:

services.AddSwaggerDocument( c =>
            {
                c.DocumentName = "apidocs";
                c.Title = "Sample API";
                c.Version = "v1";
                c.Description = "The sample API documentation description.";
                c.DocumentProcessors.Add(new SecurityDefinitionAppender("APIKey", new SwaggerSecurityScheme
                {
                    Type = SwaggerSecuritySchemeType.ApiKey,
                    Name = "APIKey",
                    In = SwaggerSecurityApiKeyLocation.Header,
                    Description = "APIKey"
                }));
                c.OperationProcessors.Add(new OperationSecurityScopeProcessor("APIKey"));
            });
5reactions
johnsanmiguelcommented, May 17, 2018

Hi,

According to the description, seems that the issue is only when trying to send the header with the token, I faced something similar, and the issue was that I didn’t include the OperationProcessor in the GeneratorSettings.

In Short, adding this line would help you:

settings.GeneratorSettings.OperationProcessors.Add(new OperationSecurityScopeProcessor("TEST_HEADER"));

Hope it helps.

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NSwag for Asp.Net Web Api 2 show security in swagger gui
We have installed nswag in our project and it works fine so far. But all our functions are secured with an api-key. when...
Read more >
API Keys
This example defines an API key named X-API-Key sent as a request header X-API-Key: <key> . The key name ApiKeyAuth is an arbitrary...
Read more >
直売所店舗 ジャケット/アウター SWAGGER Lスタジャン ...
UseSwaggerUi3 : SwaggerSecuritySchemeType.ApiKey in Header ignored ... GitHub - phillip-kruger/apiee: Swagger documentation for Java EE .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found