Open Source Web Application Framework for ASP.NET Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
3.2 KiB

using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Volo.Abp.Content;
namespace Microsoft.Extensions.DependencyInjection;
public static class AbpSwaggerGenServiceCollectionExtensions
{
public static IServiceCollection AddAbpSwaggerGen(
this IServiceCollection services,
Action<SwaggerGenOptions> setupAction = null)
{
return services.AddSwaggerGen(
options =>
{
Func<OpenApiSchema> remoteStreamContentSchemaFactory = () => new OpenApiSchema()
{
Type = "string",
Format = "binary"
};
options.MapType<RemoteStreamContent>(remoteStreamContentSchemaFactory);
options.MapType<IRemoteStreamContent>(remoteStreamContentSchemaFactory);
setupAction?.Invoke(options);
});
}
public static IServiceCollection AddAbpSwaggerGenWithOAuth(
this IServiceCollection services,
[NotNull] string authority,
[NotNull] Dictionary<string, string> scopes,
Action<SwaggerGenOptions> setupAction = null,
string authorizationEndpoint = "/connect/authorize",
string tokenEndpoint = "/connect/token",
string openIdConnectDiscoveryEndpoint = "/.well-known/openid-configuration")
{
var authorizationUrl = new Uri($"{authority.TrimEnd('/')}{authorizationEndpoint.EnsureStartsWith('/')}");
var tokenUrl = new Uri($"{authority.TrimEnd('/')}{tokenEndpoint.EnsureStartsWith('/')}");
var openIdConnectDiscoveryUrl = new Uri($"{authority.TrimEnd('/')}{openIdConnectDiscoveryEndpoint.EnsureStartsWith('/')}");
return services
.AddAbpSwaggerGen()
.AddSwaggerGen(
options =>
{
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OpenIdConnect,
OpenIdConnectUrl = openIdConnectDiscoveryUrl,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = authorizationUrl,
Scopes = scopes,
TokenUrl = tokenUrl
}
}
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
Array.Empty<string>()
}
});
setupAction?.Invoke(options);
});
}
}