master
parent
ddb4f82dbd
commit
fafa5ae02a
@ -0,0 +1,127 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace CorsServer.WebApi31
|
||||||
|
{
|
||||||
|
public class StartupConfig
|
||||||
|
{
|
||||||
|
public StartupConfig(IConfiguration configuration, IHostEnvironment hostingEnvironment, IWebHostEnvironment webHostEnvironment)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
//Config
|
||||||
|
services.Configure<CorsOption>(Configuration.GetSection("CORS"));
|
||||||
|
|
||||||
|
//Cors配置文件选项
|
||||||
|
AddCors_Config(services);
|
||||||
|
|
||||||
|
services.AddControllers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsSnapshot<CorsOption> corsOtionsSnapshot)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseCors(CorsPolicyNameConst.DefaultPolicyName);
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 注册不同的Cors策略
|
||||||
|
|
||||||
|
private IServiceCollection AddCors_Config(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddCors(setup =>
|
||||||
|
{
|
||||||
|
var corsOption = services.BuildServiceProvider().GetRequiredService<IOptionsSnapshot<CorsOption>>().Value;
|
||||||
|
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, builder =>
|
||||||
|
{
|
||||||
|
if (corsOption.Origins == null)
|
||||||
|
{
|
||||||
|
builder.SetIsOriginAllowed(_ => true);
|
||||||
|
}
|
||||||
|
else if (corsOption.Origins.Count == 0)
|
||||||
|
{
|
||||||
|
builder.SetIsOriginAllowed(_ => true);
|
||||||
|
}
|
||||||
|
else if (corsOption.Origins.Contains("*"))
|
||||||
|
{
|
||||||
|
builder.SetIsOriginAllowed(_ => true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.WithOrigins(corsOption.Origins.ToArray());
|
||||||
|
builder.SetIsOriginAllowedToAllowWildcardSubdomains();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (corsOption.Methods == null || corsOption.Methods.Count == 0)
|
||||||
|
{
|
||||||
|
builder.AllowAnyMethod();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.WithMethods(corsOption.Methods.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (corsOption.Headers == null || corsOption.Headers.Count == 0)
|
||||||
|
{
|
||||||
|
builder.AllowAnyHeader();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.WithMethods(corsOption.Headers.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (corsOption.ExposedHeaders != null && corsOption.ExposedHeaders.Count > 0)
|
||||||
|
{
|
||||||
|
builder.WithExposedHeaders(corsOption.ExposedHeaders.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (corsOption.AllowCredentials)
|
||||||
|
{
|
||||||
|
builder.AllowCredentials();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.DisallowCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (corsOption.PreflightMaxAge.TotalSeconds > 0)
|
||||||
|
{
|
||||||
|
builder.SetPreflightMaxAge(corsOption.PreflightMaxAge);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace CorsServer.WebApi31
|
||||||
|
{
|
||||||
|
public class StartupDefaultPolicy
|
||||||
|
{
|
||||||
|
public StartupDefaultPolicy(IConfiguration configuration, IHostEnvironment hostingEnvironment, IWebHostEnvironment webHostEnvironment)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
//config
|
||||||
|
services.Configure<CorsOption>(Configuration.GetSection("CORS"));
|
||||||
|
|
||||||
|
//Cors
|
||||||
|
AddDefaultCors(services);
|
||||||
|
|
||||||
|
services.AddControllers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsSnapshot<CorsOption> corsOtionsSnapshot)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
//根路径:全局访问前辍 http://www.custom.com/PathBase/
|
||||||
|
//app.UsePathBase("/api/");
|
||||||
|
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseCors();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置默认策略Cors
|
||||||
|
/// </summary>
|
||||||
|
private IServiceCollection AddDefaultCors(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddCors(setupCors =>
|
||||||
|
{
|
||||||
|
setupCors.AddDefaultPolicy(build =>
|
||||||
|
{
|
||||||
|
build
|
||||||
|
.AllowAnyOrigin()
|
||||||
|
.AllowAnyMethod()
|
||||||
|
.AllowAnyHeader()
|
||||||
|
.SetPreflightMaxAge(TimeSpan.FromMinutes(10))
|
||||||
|
;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue