Web API documentation using swagger

I am new to swagger, I saw a couple of online documentation for implementing Swagger for my webapi and used some SwaggerConfig properties to configure.

Here is my requirement: - I need to show the swagger documentation only when I click the Explore button based on the API_Key TextBox value, which should match my app.config key.

enter image description here

So, I see to implement like this: -

  • The user comes to my home page, by default he will have empty documentation, except for the title, as shown in the image.
  • Enters API_Key into the text field contained in the header and clicks "Explore".
  • The entered API key is checked using the key in my app.config or, if possible, in SwaggerConfig.
  • If the confirmed displayed documentation still displays the error message as an invalid API key.

Need suggestions.

Regards, Ragu

+1
c # swagger-ui swashbuckle
Aug 27 '15 at 14:44
source share
2 answers

Just edit index.html and add the desired headers to the addApiKeyAuthorization change addApiKeyAuthorization . See here for more details:

https://github.com/swagger-api/swagger-ui#header-parameters

+2
Aug 28 '15 at 2:34
source share

You can add your own message handler for the web api, and then execute authorized documentation requests:

  private const string swaggerApikey = "swagger-apiKey"; private void Configuration([NotNull] IAppBuilder app) { var config = new HttpConfiguration(); config.MessageHandlers.Add(new SwaggerMessageHandler()); config .EnableSwagger(c => { c.ApiKey(swaggerApikey) .Description(swaggerApikey) .Name(swaggerApikey) .In("header"); }) .EnableSwaggerUi(c => { c.EnableApiKeySupport(swaggerApikey, "header"); }); app.UseWebApi(config); } internal class SwaggerMessageHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.RequestUri.LocalPath.Equals("/swagger/docs/v1")) { var apikey = request.Headers.FirstOrDefault(x => x.Key.Equals(swaggerApikey)).Value?.FirstOrDefault(); if (!"secretApiKey".Equals(apikey)) return Task.FromResult(new HttpResponseMessage(HttpStatusCode.Forbidden)); } return base.SendAsync(request, cancellationToken); } } 
+1
Apr 19 '17 at 9:24 on
source share



All Articles