public static IApplicationBuilder UseCoreSwagger(this IApplicationBuilder app, AspnetCoreOptions aspnetCoreOptions = null) { var entryAssembly = Assembly.GetEntryAssembly(); string assemblyProduct; string version; if (entryAssembly != null) { assemblyProduct = entryAssembly.GetCustomAttribute <AssemblyProductAttribute>()?.Product; version = $"V{entryAssembly.GetName().Version!.Major}"; } else { assemblyProduct = ".Net Core API"; version = "1.0.0.0"; } app.UseSwagger(options => { options.PreSerializeFilters.Add((swaggerDoc, httpReq) => { swaggerDoc.Servers = new List <OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}" } }; }); }); app.UseSwaggerUI(options => { options.DocumentTitle = assemblyProduct; options.DisplayRequestDuration(); if (aspnetCoreOptions?.ClientSecurity != null) { options.OAuthClientId(aspnetCoreOptions.ClientSecurity.ClientId); options.OAuthClientSecret(aspnetCoreOptions.ClientSecurity.ClientSecret); } options.SwaggerEndpoint("/swagger/" + version + "/swagger.json", version); }); app.UseReDoc(options => { options.SpecUrl = "/swagger/" + version + "/swagger.json"; options.DocumentTitle = assemblyProduct; }); return(app); }
private static void AddLocalizationSupport(this IServiceCollection services, AspnetCoreOptions aspnetCoreOptions) { services.AddLocalization(options => { options.ResourcesPath = "Resources"; }); if (aspnetCoreOptions.SupportedCultures.Any()) { services.Configure <RequestLocalizationOptions>(options => { var distinctCultureNames = aspnetCoreOptions.SupportedCultures .Union(new[] { CultureInfo.CurrentCulture.Name }) .Distinct(StringComparer.InvariantCultureIgnoreCase); var supportedCultures = distinctCultureNames.Select(x => new CultureInfo(x)).ToList(); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); } }
public static IServiceCollection AddCore(this IServiceCollection services, IConfiguration configuration, Action <AspnetCoreOptions> configureOptions = null) { services.Configure <AspnetCoreOptions>(configuration.GetSection(ASPNET_CORE_OPTIONS), binder => binder.BindNonPublicProperties = true); if (configureOptions != null) { services.PostConfigure(configureOptions); } var aspnetCoreOptions = services.BuildServiceProvider().GetService <IOptionsSnapshot <AspnetCoreOptions> >() ?.Value; if (aspnetCoreOptions == null) { aspnetCoreOptions = new AspnetCoreOptions { Cors = new CorsOptions() }; aspnetCoreOptions.SupportedCultures.Add(DEFAULT_CULTURE); } services.AddLocalizationSupport(aspnetCoreOptions); services.AddControllers(options => { var policy = new AuthorizationPolicyBuilder(BEARER).RequireAuthenticatedUser().Build(); options.Filters.Add(new AuthorizeFilter(policy)); aspnetCoreOptions?.MvcOptions?.Invoke(options); }).AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); services.AddJwtAuthentication(aspnetCoreOptions.Jwt); services.AddSwagger(aspnetCoreOptions.ClientSecurity); services.AddCorsSupport(aspnetCoreOptions); return(services); }
private static void AddCorsSupport(this IServiceCollection services, AspnetCoreOptions aspnetCoreOptions) { services.AddCors(delegate(Microsoft.AspNetCore.Cors.Infrastructure.CorsOptions options) { options.AddDefaultPolicy(delegate(CorsPolicyBuilder builder) { if (aspnetCoreOptions.Cors.Origins.Any()) { builder.WithOrigins(aspnetCoreOptions.Cors.Origins.ToArray()); } if (aspnetCoreOptions.Cors.Headers.Any()) { builder.WithHeaders(aspnetCoreOptions.Cors.Headers.ToArray()); } if (aspnetCoreOptions.Cors.Methods.Any()) { builder.WithMethods(aspnetCoreOptions.Cors.Methods.ToArray()); } }); }); }