6

I am using asp.netcore 3.1 and openapi 3.0.1

I have added authorization to my apis using the following code:

services.AddSwaggerGen(setupAction =>
        {
            setupAction.SwaggerDoc("APIs_Documentation", new OpenApiInfo
            {
                Title = "Project APIs",
                Version = "1"
            });

            setupAction.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
            {
                Type = SecuritySchemeType.Http,
                Scheme = "bearer",
                Description = "Enter token here",
                Name = "Authorization",
                @In = ParameterLocation.Header,

            });
            setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference
                        {
                        Type = ReferenceType.SecurityScheme,
                        Id = "oauth2",

                        }
                    },new List<string>()}
        });
        });

Is there any way to set a default value to the value field of the popup authorization dialogue in the next image?

enter image description here

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mohamed Hasan
  • 207
  • 4
  • 13
  • In standalone Swagger UI this is done using [`preauthorizeApiKey`](https://stackoverflow.com/a/50181701/113116). Check if Swashbuckle has an option to add a `preauthorizeApiKey` call or if it lets you customize the code of Swagger UI's HTML page. – Helen Nov 17 '20 at 12:20
  • You can try to see if you can provide a default identity in the middleware and provide a default token to write to context.user. – Yinqiu Nov 17 '20 at 14:05
  • I have the same problem, and I tried to return my token with Bearer attached to it like this: Bearer [myToken] – Omid Rostami May 31 '21 at 08:22

1 Answers1

0

If you are looking for a way to add a default header you can do it from C# like this:

app.UseSwagger()
.UseSwaggerUI(c =>

{
    c.Interceptors = new InterceptorFunctions
    {
        RequestInterceptorFunction = "function (req) { req.headers['MyCustomHeader'] = 'CustomValue'; return req; }"
    };
}

);

or want to add it to Swagger UI: related question