- 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
73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Authentication.Negotiate;
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
|
|
.AddNegotiate();
|
|
builder.Services.AddAuthorization();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
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();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
// Enable sending credentials (NTLM tokens) with Swagger UI requests
|
|
options.ConfigObject.AdditionalItems["withCredentials"] = true;
|
|
});
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|