SwaggerGenOptionsExtensions Class
Definition
- Namespace
- Codebelt.Extensions.Swashbuckle.AspNetCore
- Assemblies
- Codebelt.Extensions.Swashbuckle.AspNetCore.dll
Extension methods for the SwaggerGenOptions class.
public static class SwaggerGenOptionsExtensions
- Inheritance
-
objectSwaggerGenOptionsExtensions
Examples
Add security schemes and user-agent documentation to Swagger API specifications. These extension methods are called within the AddSwaggerGen(options => { ... }) setup action to document security mechanisms and required headers. The example shows how to compose multiple security scheme extensions together to document API Key, JWT Bearer, and Basic Authentication schemes, plus an additional user-agent header requirement. This declares the security options to the API consumer and generates the appropriate OpenAPI documentation:
using System;
using Codebelt.Extensions.Swashbuckle.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace MySwaggerExample;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
// Add security scheme documentation
options.AddXApiKeySecurity();
options.AddJwtBearerSecurity();
options.AddBasicAuthenticationSecurity();
// Add user-agent documentation
options.AddUserAgent();
});
var app = builder.Build();
if (string.Equals(app.Environment.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.Run();
}
}
Methods
| Name | Description |
|---|---|
| AddBasicAuthenticationSecurity(SwaggerGenOptions) | Adds support for AuthN/AuthZ using the Basic security scheme. |
| AddJwtBearerSecurity(SwaggerGenOptions) | Adds support for AuthN/AuthZ using the Bearer security scheme in JWT format. |
| AddUserAgent(SwaggerGenOptions, Action<UserAgentDocumentOptions>) | Adds an UserAgentDocumentFilter to the DocumentFilterDescriptors. |
| AddXApiKeySecurity(SwaggerGenOptions) | Adds support for first line of defense using security based HTTP X-Api-Key header. |