You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
9.8 KiB
C#

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;
4 years ago
using Microsoft.Extensions.Options;
namespace CorsServer.WebApi31
{
public class Startup
{
4 years ago
public Startup(IConfiguration configuration,IHostEnvironment hostingEnvironment,IWebHostEnvironment webHostEnvironment)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
4 years ago
#region Config
4 years ago
services.Configure<CorsOption>(Configuration.GetSection("CORS"));
4 years ago
#endregion
#region CORS
4 years ago
AddCors_Test(services);
4 years ago
//AddCors_2(services);
//AddCors_3(services);
//AddCors_4(services);
4 years ago
//AddCors_5(services);
4 years ago
#endregion
services.AddControllers();
}
4 years ago
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptionsSnapshot<CorsOption> corsOtionsSnapshot)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//<2F><>·<EFBFBD><C2B7><EFBFBD><EFBFBD>ȫ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ǰ<EFBFBD><C7B0> http://www.custom.com/PathBase/
//app.UsePathBase("/api/");
app.UseRouting();
4 years ago
app.UseCors(CorsPolicyNameConst.DefaultPolicyName);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
4 years ago
4 years ago
/// <summary>
/// ȫ<><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>
/// </summary>
private IServiceCollection AddCors_Info(IServiceCollection services)
4 years ago
{
services.AddCors(setup =>
{
4 years ago
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
4 years ago
{
build
4 years ago
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
.AllowAnyOrigin() //<2F><><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
//.WithOrigins() //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
.SetIsOriginAllowed(_ => true) //ʹ<><CAB9>Func<string bool> ί<>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>
.SetIsOriginAllowedToAllowWildcardSubdomains() //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4>ʹ<EFBFBD><CAB9>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>(*<2A><>)
//<2F><><EFBFBD>󷽷<EFBFBD>(POST GET PUT DELETE OPTIONS<4E><53>)
.AllowAnyMethod() //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD>
//.WithMethods() //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD>ͷ
.AllowAnyHeader() //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
//.WithHeaders() //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
//ƾ<><C6BE>
.AllowCredentials() //<2F><><EFBFBD><EFBFBD>ƾ<EFBFBD>ݣ<EFBFBD>֤<EFBFBD><D6A4><EFBFBD>а<EFBFBD><D0B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(cookies)<29><>HTTP<54><50>֤Э<D6A4><D0AD>(HTTP authentication schemes)
//.DisallowCredentials() //<2F>ܾ<EFBFBD>ƾ<EFBFBD><C6BE>
//.WithExposedHeaders() //<2F><><EFBFBD>ñ<EFBFBD><EFBFBD><C2B6><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>Ӧͷ<D3A6><CDB7>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>ᱩ¶Ĭ<C2B6>ϵ<EFBFBD><CFB5><EFBFBD>Ӧͷ<D3A6><CDB7>Ӧ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>Ӱ<EFBFBD><D3B0>ͷ<EFBFBD><CDB7><EFBFBD>ᱩ¶<E1B1A9><C2B6>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
;
/*<EFBFBD>ر<EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ڰ<EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>.net core 2.1<EFBFBD><EFBFBD>ʼ, AllowAnyOrigin() <EFBFBD><EFBFBD> AllowCredentials() <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱʹ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
1<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>AllowCredentials()ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.SetIsOriginAllowed(_ => true) <EFBFBD><EFBFBD><EFBFBD><EFBFBD> AllowAnyOrigin()
2<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>AllowCredentials()ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> WithOrigins()ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ(ʹ<EFBFBD><EFBFBD>SetIsOriginAllowedToAllowWildcardSubdomains()<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>) <EFBFBD><EFBFBD><EFBFBD><EFBFBD> AllowAnyOrigin()
3<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD>
*/
});
});
return services;
}
/// <summary>
/// CORS ģ<><C4A3>
/// </summary>
private IServiceCollection AddCors_Template(IServiceCollection services)
{
services.AddCors(setup =>
{
var corsOption = services.BuildServiceProvider().GetRequiredService<IOptionsSnapshot<CorsOption>>().Value;
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
{
build
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
4 years ago
.AllowAnyOrigin()
4 years ago
//.WithOrigins(corsOption.Origins.ToArray())
//.SetIsOriginAllowed(_ => true)
//.SetIsOriginAllowedToAllowWildcardSubdomains()
//<2F><><EFBFBD>󷽷<EFBFBD>(POST GET PUT DELETE OPTIONS<4E><53>)
4 years ago
.AllowAnyMethod()
4 years ago
//.WithMethods(corsOption.Methods.ToArray())
//<2F><><EFBFBD><EFBFBD>ͷ
4 years ago
.AllowAnyHeader()
4 years ago
//.WithHeaders(corsOption.Headers.ToArray())
//ƾ<><C6BE>
//.AllowCredentials()
//.DisallowCredentials()
//.WithExposedHeaders()
;
4 years ago
});
});
return services;
}
4 years ago
/// <summary>
/// <20><><EFBFBD><EFBFBD>
/// </summary>
private IServiceCollection AddCors_Test(IServiceCollection services)
{
services.AddCors(setup =>
{
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
{
build
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
.AllowAnyOrigin()
//.WithOrigins()
//.SetIsOriginAllowed(_ => true)
//.SetIsOriginAllowedToAllowWildcardSubdomains()
//<2F><><EFBFBD>󷽷<EFBFBD>(POST GET PUT DELETE OPTIONS<4E><53>)
.AllowAnyMethod()
//.WithMethods()
//<2F><><EFBFBD><EFBFBD>ͷ
.AllowAnyHeader()
//.WithHeaders()
//ƾ<><C6BE>
//.AllowCredentials()
//.DisallowCredentials()
//.WithExposedHeaders()
;
});
});
return services;
}
private IServiceCollection AddCors_Single(IServiceCollection services)
{
services.AddCors(setup =>
{
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
{
build
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
.AllowAnyOrigin()
//.WithOrigins()
//.SetIsOriginAllowed(_ => true)
//.SetIsOriginAllowedToAllowWildcardSubdomains()
//<2F><><EFBFBD>󷽷<EFBFBD>(POST GET PUT DELETE OPTIONS<4E><53>)
//.AllowAnyMethod()
//.WithMethods()
//<2F><><EFBFBD><EFBFBD>ͷ
//.AllowAnyHeader()
//.WithHeaders()
//ƾ<><C6BE>
//.AllowCredentials()
//.DisallowCredentials()
//.WithExposedHeaders()
;
});
});
return services;
}
private IServiceCollection AddCors_All(IServiceCollection services)
4 years ago
{
services.AddCors(setup =>
{
4 years ago
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
4 years ago
{
4 years ago
build
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
;
4 years ago
});
});
return services;
}
private IServiceCollection AddCors_3(IServiceCollection services)
{
services.AddCors(setup =>
4 years ago
{
var corsOption = services.BuildServiceProvider().GetRequiredService<IOptionsSnapshot<CorsOption>>().Value;
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
{
build
.WithOrigins(corsOption.Origins.ToArray())
.WithMethods(corsOption.Methods.ToArray())
.WithHeaders(corsOption.Headers.ToArray())
.WithExposedHeaders(corsOption.ExposedHeaders.ToArray());
});
});
4 years ago
return services;
}
private IServiceCollection AddCors_4(IServiceCollection services)
{
services.AddCors(setup =>
{
4 years ago
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, build =>
4 years ago
{
build.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithExposedHeaders("x-custom-error");
});
});
return services;
}
4 years ago
private IServiceCollection AddCors_5(IServiceCollection services)
{
services.AddCors(setup =>
{
var corsOption = services.BuildServiceProvider().GetRequiredService<IOptionsSnapshot<CorsOption>>().Value;
setup.AddPolicy(CorsPolicyNameConst.DefaultPolicyName, builder =>
{
builder
////.SetIsOriginAllowedToAllowWildcardSubdomains()
.WithOrigins("http://localhost:5002")
//.AllowAnyMethod()
////.WithMethods(corsOption.Methods.ToArray())
//.AllowAnyHeader()
////.WithHeaders(corsOption.Headers.ToArray())
////.WithExposedHeaders(corsOption.ExposedHeaders.ToArray())
;
});
});
return services;
}
}
}