Enhance Swagger UI with Negotiate auth and XML docs

- Add custom OpenAPI doc with title, version, and description
- Define "Negotiate" security scheme for NTLM/Kerberos auth
- Require Negotiate authentication for all endpoints in Swagger
- Include XML comments in Swagger UI if available
- Configure Swagger UI to send credentials (withCredentials: true) for authenticated endpoint testing
This commit is contained in:
2026-03-13 10:37:16 +01:00
parent 5b37dbf854
commit 7926d3d93f
2 changed files with 43 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
@@ -11,7 +12,42 @@ builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
builder.Services.AddAuthorization();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "FakeNTLMServer",
Version = "v1",
Description = "NTLM/Negotiate authentication test server"
});
options.AddSecurityDefinition("Negotiate", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "Negotiate",
Description = "Windows Authentication (NTLM/Kerberos). Credentials are sent automatically by the browser."
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Negotiate"
}
},
Array.Empty<string>()
}
});
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
options.IncludeXmlComments(xmlPath);
});
var app = builder.Build();
@@ -19,7 +55,11 @@ var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerUI(options =>
{
// Enable sending credentials (NTLM tokens) with Swagger UI requests
options.ConfigObject.AdditionalItems["withCredentials"] = true;
});
}
app.UseHttpsRedirection();