零度框架升级到 Aspire 13.0 版本
This commit is contained in:
1 parent
41f6c147d5
commit
b11ba4f6a5
50 files changed
+336
-446
No files matched your search
@@ -3,9 +3,10 @@
|
||||
|
||||
using HelloShop.ServiceDefaults.Infrastructure;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.OpenApi;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.Swagger;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
@@ -15,54 +16,75 @@ namespace HelloShop.ServiceDefaults.Extensions
|
||||
{
|
||||
public static class OpenApiExtensions
|
||||
{
|
||||
public static IServiceCollection AddOpenApi(this IServiceCollection services, Action<SwaggerGenOptions>? configureOptions = null)
|
||||
public static IServiceCollection AddOpenApiServices(this IServiceCollection services, Action<OpenApiOptions>? configureOptions = null)
|
||||
{
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(configureOptions);
|
||||
|
||||
services.Configure<SwaggerUIOptions>(options =>
|
||||
services.AddOpenApi(options =>
|
||||
{
|
||||
options.DocumentTitle = Assembly.GetEntryAssembly()?.GetName().Name;
|
||||
options.InjectStylesheet("/ServiceDefaults/Resources/OpenApi/Custom.css");
|
||||
});
|
||||
|
||||
services.Configure<SwaggerGenOptions>(options =>
|
||||
{
|
||||
options.OperationFilter<AcceptLanguageHeaderOperationFilter>();
|
||||
|
||||
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme
|
||||
{
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
BearerFormat = "JWT",
|
||||
Description = "JWT Authorization header using the Bearer scheme."
|
||||
});
|
||||
|
||||
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" }
|
||||
},
|
||||
Array.Empty<string>()
|
||||
}
|
||||
});
|
||||
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
|
||||
options.AddDocumentTransformer<AcceptLanguageHeaderDocumentTransformer>();
|
||||
configureOptions?.Invoke(options);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseOpenApi(this IApplicationBuilder app, Action<SwaggerOptions>? apiConfigureOptions = null, Action<SwaggerUIOptions>? uiConfigureOptions = null)
|
||||
internal sealed class BearerSecuritySchemeTransformer : IOpenApiDocumentTransformer
|
||||
{
|
||||
app.UseSwagger(apiConfigureOptions);
|
||||
app.UseSwaggerUI(uiConfigureOptions);
|
||||
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
var securityScheme = new OpenApiSecurityScheme
|
||||
{
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
BearerFormat = "JWT",
|
||||
Description = "JWT Authorization header using the Bearer scheme."
|
||||
};
|
||||
|
||||
document.Components ??= new OpenApiComponents();
|
||||
document.Components.SecuritySchemes = new Dictionary<string, IOpenApiSecurityScheme>
|
||||
{
|
||||
["bearerAuth"] = securityScheme
|
||||
};
|
||||
|
||||
var securitySchemeReference = new OpenApiSecuritySchemeReference("bearerAuth", document);
|
||||
|
||||
var securityRequirement = new OpenApiSecurityRequirement
|
||||
{
|
||||
[securitySchemeReference] = []
|
||||
};
|
||||
|
||||
foreach (var path in document.Paths.Values)
|
||||
{
|
||||
if (path.Operations != null)
|
||||
{
|
||||
foreach (var operation in path.Operations.Values)
|
||||
{
|
||||
operation.Security ??= [];
|
||||
operation.Security.Add(securityRequirement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public static WebApplication UseOpenApiWithUI(this WebApplication app, Action<SwaggerUIOptions>? uiConfigureOptions = null)
|
||||
{
|
||||
app.MapOpenApi();
|
||||
|
||||
app.Map("/ServiceDefaults", appBuilder => appBuilder.UseStaticFiles(new StaticFileOptions
|
||||
{
|
||||
FileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly())
|
||||
}));
|
||||
|
||||
app.UseSwaggerUI(options => {
|
||||
options.DocumentTitle = Assembly.GetEntryAssembly()?.GetName().Name;
|
||||
options.InjectStylesheet("/ServiceDefaults/Resources/OpenApi/Custom.css");
|
||||
options.SwaggerEndpoint("/openapi/v1.json", "v1");
|
||||
uiConfigureOptions?.Invoke(options);
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user