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:
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
|||||||
44
Program.cs
44
Program.cs
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Authentication.Negotiate;
|
using Microsoft.AspNetCore.Authentication.Negotiate;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@@ -11,7 +12,42 @@ builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
|
|||||||
builder.Services.AddAuthorization();
|
builder.Services.AddAuthorization();
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
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();
|
var app = builder.Build();
|
||||||
|
|
||||||
@@ -19,7 +55,11 @@ var app = builder.Build();
|
|||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI();
|
app.UseSwaggerUI(options =>
|
||||||
|
{
|
||||||
|
// Enable sending credentials (NTLM tokens) with Swagger UI requests
|
||||||
|
options.ConfigObject.AdditionalItems["withCredentials"] = true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|||||||
Reference in New Issue
Block a user