Reputation: 21
In swagger there is a .BasicAuth("basic").Description("Basic HTTP Authorization") line that I uncommented.
However when trying to use one of the endpoints, upon clicking the "Try it out!" button I am prompted with logging in. No matter what I put in this box (username and password boxes) I cannot run or test the endpoint because I am unauthorized. What do I need to do inside the SwaggerConfig.cs file or elsewhere to make it so I can test the endpoints?
I am using .Net Framework not .Net Core and I do not have preauthorizeBasic() extension that I have seen used elsewhere.
In my web.config file I have 2 API keys, one for user and one for password but I do not know how to use these to authenticate swagger in the SwaggerConfig.cs file.
To me, it looks like uncommenting the .BasicAuth("basic") line does nothing to the Swagger UI. Not including it or including it makes no difference. I still am prompted to login to my "localhost:" page and I can't get past this box.
I am pretty positive I need to setup BasicAuth to take in those 2 API keys for username and password but I don't know where to put these values because even if I put them in the login box it does not work.
Thank you for any help...
Upvotes: 2
Views: 7106
Reputation: 352
Could you show us a piece of code, in your startup you should have something like this :
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "BasicAuth", Version = "v1" });
c.AddSecurityDefinition("basic", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "basic",
In = ParameterLocation.Header,
Description = "Basic Authorization header using the Bearer scheme."
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "basic"
}
},
new string[] {}
}
});
});
In the UI you should see an "Authorize" button with a padlock
clicking on it shows a popup
Then when you test your apis the basic header is sent
This tuto can be helpfull
Upvotes: 4