How to apply header in api documentation

45 views
Skip to first unread message

RAJAN KUMAR

unread,
Sep 22, 2017, 11:45:39 PM9/22/17
to Swagger
I have used swagger in .net mvc 5 webapi for api documetation. It is working fine for all the verbs like: get, post,put, delete. But I want to pass header(authentication) key in all the apis for security purpose. Please help me how I implement thi

RAJAN KUMAR

unread,
Sep 25, 2017, 9:29:08 AM9/25/17
to Swagger
Please someone reply. I have to implement this.

Ron Ratovsky

unread,
Sep 27, 2017, 2:52:53 PM9/27/17
to swagger-sw...@googlegroups.com

I assume you use Swashbuckle which is a community project – we’re unable to provide support to it. Please file a ticket with the project directly.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

RAJAN KUMAR

unread,
Oct 5, 2017, 10:36:46 AM10/5/17
to Swagger




down voteaccepted

Yes you can do it via inheriting from IOperationFilter

You can find the answer on github here: AddRequiredHeaderParameter

   public class AddRequiredHeaderParameter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.parameters == null)
            operation.parameters = new List<Parameter>();

        operation.parameters.Add(new Parameter
        {
            name = "X-User-Token",
            @in = "header",
            type = "string",
            required = false
        });
    }
}

Then you go to your SwaggerConfig.cs file and add the following in the EnableSwagger section:

   c.OperationFilter(() => new AddRequiredHeaderParameter());

Thanks to stackoverflow :)

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages