-
Notifications
You must be signed in to change notification settings - Fork 679
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
How do I tell swashbuckle to add a required header #501
Comments
HI there, If you are customizing the index.html page then you can add your own header by changing the addApiKeyAuthorization javascript function in the index.html page and by adding below properties in the SwaggerConfig.cs class. SwaggerConfig.cs config.EnableSwagger( function addApiKeyAuthorization() {
|
In addition, if you'd like to list the header as a parameter with each operation description, you can just wire it up via an IOperationFilter (see readme). Here's some sample code to get you started:
|
it does not close all use cases at all. |
I know this is sort of old, but I will respond anyway, as I was led here by some google-fu
If you document your actions with [HttpGet], [HttpPut], [HttpPost] etc.
|
My swagger UI is generated automatically.
|
Following upon @VisualBean 's example, I created this special header for my OData V4 patch requests.
|
If you copy and paste below code SwaggerConfig.cs, it will work.
|
@abhargavasharma it is not coming on the UI. I have tried all the above solutions. |
Create a new Class named CustomFilters in the namespaceFooSpace.Filters and add the following code.
In Startup.cs, in the
|
Actually I am using oAuth2 with password flow. It will be helpful , if you
can provide the example for same.
Currently no value is passed in header i.e. username and password.
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1",
"Misys.OpicsPlus.Framework.Rest.ApplicationService");
c.UseFullTypeNameInSchemaIds();
c.BasicAuth("basic").Description("HTTP Authentication
Method");
c.OAuth2("oauth2")
.Description("OAuth2 Grant")
.Flow("password")
.TokenUrl("
https://BLR2TCM29.misys.global.ad/SecureTokenServiceRestV4.0/api/authorization/requestaccesstoken
")
.Scopes(scopes =>
{
scopes.Add("acccess", "try out the sample
api");
}
);
c.OperationFilter<AddRequiredAuthorizationHeaderParameter>();
})
.EnableSwaggerUi(c =>
{
c.EnableOAuth2Support(
clientId: "Sample_App",
clientSecret: "xxxxx",
realm: "test-realm",
appName: "Swagger UI");
});
}
public class AddRequiredAuthorizationHeaderParameter : IOperationFilter
{
public void Apply(Swashbuckle.Swagger.Operation operation,
SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var actFilters =
apiDescription.ActionDescriptor.GetFilterPipeline();
var allowsAnonymous = actFilters.Select(f =>
f.Instance).OfType<OverrideAuthorizationAttribute>().Any();
if (allowsAnonymous)
return; // must be an anonymous method
//var scopes =
apiDescription.ActionDescriptor.GetFilterPipeline()
// .Select(filterInfo => filterInfo.Instance)
// .OfType<AllowAnonymousAttribute>()
// .SelectMany(attr => attr.Roles.Split(','))
// .Distinct();
if (operation.security == null)
operation.security = new List<IDictionary<string,
IEnumerable<string>>>();
var oAuthRequirements = new Dictionary<string,
IEnumerable<string>>
{
{"oauth2", Enumerable.Empty<string>()}
};
operation.security.Add(oAuthRequirements);
}
}
Any help is highly appreciated.
Regards,
Ayesha
…On Mon, Aug 27, 2018 at 9:37 PM Jordan Griffiths ***@***.***> wrote:
@ayeshathecoder <https://github.com/ayeshathecoder>
Create a new Class named CustomFilters in the namespaceFooSpace.Filters
and add the following code.
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
namespace FooSpace.Filters
{
public class CustomFilters
{
public class AuthHeaderFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
if (operation.Parameters == null)
operation.Parameters = new List<IParameter>();
operation.Parameters.Add(new NonBodyParameter
{
Name = "Authorization",
In = "header",
Type = "string",
Required = false,
Default = "Bearer "
});
}
}
}
}
In Startup.cs, in the ConfigureServices()method add the following lines
of code after services.AddMvc();:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });
c.OperationFilter<Filters.CustomFilters.AuthHeaderFilter>();
});
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#501 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ANF9IHPQzBRIQ4Ma7CSCp2Sysql88oZYks5uVBlQgaJpZM4F--5R>
.
|
This works great but is there a way to do this and then exclude it from one or two places? |
Can anyone suggest me how to add token parameter to get method? |
@cward-ADC is what i used to do add a token parameter. |
Can I add a required parameter without generics? |
It works only for post method, how about get ? |
[HttpGet]
public IActionResult Get([FromHeader(Name = "Foo-Header")] string mediaType)
{
// TODO: something
} |
I don't know how this works for you guys, but my OperationFilter is applied AFTER the request, which means first my Web API Controller is called and THEN this operation filter runs. Actually I wanted to have swagger add some custom headers to the request. Everyone says IOperationFilter is the answer, but I'm pretty sure it's not. |
Got it, know how it is intended to work now. |
Também gostaria de saber como funciona, estou com o mesmo problema. |
I have one of my API controller that requires a header to be present (this is not a security header).
From what I've found on the internet (http://stackoverflow.com/questions/26742521/sending-dynamic-custom-headers-in-swagger-ui-try-outs), in java you can use http://docs.swagger.io/swagger-core/apidocs/com/wordnik/swagger/annotations/ApiImplicitParam.html with a paramType="header"
How do I do the same in Swashbuckle ?
The text was updated successfully, but these errors were encountered: