简体   繁体   中英

Swagger OAuth, how to send bearer token in a different header than "Authorization"

I have an API with authorization code flow authentication and I have configured swagger to use that as a security definition and it works fine. Now I need swagger to send the bearer token in a different header as well, besides "Authorization", eg "X-Forwarded-Authorization". Is there a way to do that?

My current security configuration:

setup.AddSecurityDefinition(
    "oauth2", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.OAuth2,
        Flows = new OpenApiOAuthFlows
        {
            AuthorizationCode = new OpenApiOAuthFlow
            {
                AuthorizationUrl = new Uri("..."),
                TokenUrl = new Uri("..."),
                Scopes = { }
            }
        },
    });

You can configure swagger when adding service collection like this;

services.AddSwaggerGen(options =>
{
    //...other configurations

    options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Description = "Please insert JWT with Bearer into field!",
        Name = "X-Forwarded-Authorization",
        Type = SecuritySchemeType.ApiKey
    });

    options.AddSecurityRequirement(new OpenApiSecurityRequirement 
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                  Type = ReferenceType.SecurityScheme,
                  Id = "Bearer"
                }
            }, Array.Empty<string>() 
        }
    });

    //...other configurations
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM