Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger authentication #1

Closed
GVDmeijde opened this issue Dec 11, 2019 · 3 comments
Closed

Swagger authentication #1

GVDmeijde opened this issue Dec 11, 2019 · 3 comments

Comments

@GVDmeijde
Copy link

@GVDmeijde GVDmeijde commented Dec 11, 2019

Hi Josef,

I am trying to get my swagger to send the x-api-key in its header, but i cant get it to work.
Do you maybe know how to connect the swagger authentication to this API authentication method?

My latest tries where by using the 'securitydefinition' as shown in the snippet below.
c.AddSecurityDefinition("API KEY", new OpenApiSecurityScheme { Name = "X-API-KEY", Description = "ApiKey must appear in header", Type = SecuritySchemeType.ApiKey, In = ParameterLocation.Header });
Unfortunately, this was without success :(

I hope you can help me,
Thanks in advance,
Gijs

@joseftw

This comment has been minimized.

Copy link
Owner

@joseftw joseftw commented Dec 12, 2019

Yes I do :)
Will see if I have time to create a quick blog post later, or just update this repo with CODE

@joseftw

This comment has been minimized.

Copy link
Owner

@joseftw joseftw commented Dec 14, 2019

I dont have time to create a blog post right now but try this

ApiKeyConstants.HeaderName is just a string, "X-Api-Key"

public static void Configure(IServiceCollection services)
{
    services.AddSwaggerGen(c =>
    {
            c.AddSecurityDefinition(ApiKeyConstants.HeaderName, new OpenApiSecurityScheme
            {
                Description = "Api key needed to access the endpoints. X-Api-Key: My_API_Key",
                In = ParameterLocation.Header,
                Name = ApiKeyConstants.HeaderName,
                Type = SecuritySchemeType.ApiKey
            });

            c.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                { new OpenApiSecurityScheme
                {
                    Name = ApiKeyConstants.HeaderName,
                    Type = SecuritySchemeType.ApiKey,
                    In = ParameterLocation.Header,
                    Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = ApiKeyConstants.HeaderName }
                }, new string[] {}}
            });
        });
    } `
@joseftw joseftw closed this Dec 14, 2019
@joseftw

This comment has been minimized.

Copy link
Owner

@joseftw joseftw commented Dec 15, 2019

@joseftw joseftw reopened this Dec 15, 2019
@joseftw joseftw closed this Dec 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.