Token-based authentication support in the Swagger documentation for the web API

I am trying to use swagger ( SwashBuckle ) to create web api documentation. I have successfully generated the documentation using the web interface documentation using swagger , but I cannot successfully send API requests since we have authentication on tokens (a custom header for authentication purposes) for several methods.

I tried to figure out an example of code / resources for the same, but didn't have much luck. Please let me know if someone has applied / met similar in their application.

+4
source share
2 answers

, http://bitoftech.net/2014/08/25/asp-net-web-api-documentation-using-swagger/ .

, :

1. "SwaggerExtensions", JS "onComplete.js", "Embedded Resource".

2. "onComplete.js" :

$('#input_apiKey').change(function () {

var key = $('#input_apiKey')[0].value;
if (key && key.trim() != "") {
key = "Bearer " + key;
window.authorizations.add("key", new ApiKeyAuthorization("Authorization", key, "header"));
}
});

3. "SwaggerConfig.cs" register :

SwaggerUiConfig.Customize(c =>
{
c.SupportHeaderParams = true;
c.InjectJavaScript(typeof(SwaggerConfig).Assembly, "AngularJSAuthentication.API.SwaggerExtensions.onComplete.js");
});

, .

, , , .

+8

API. " /" README https://github.com/domaindrivendev/Swashbuckle , - httpConfiguration.EnableSwagger()

c.ApiKey("apiKey")
  .Description("API Key Authentication")
  .Name("apiKey")
  .In("header");

, IDocumentFilter, . , ApiKeyFilter. Swagger, EnableSwagger()

c.OperationFilter<ApiKeyFilter>();

ApiKey, https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Dummy.Core/SwaggerExtensions/AssignOAuth2SecurityRequirements.cs OAuth2, .

+4

All Articles