From 2268ad0c8c2dc8cb2707062965dfcf0f31ad1787 Mon Sep 17 00:00:00 2001 From: xRain Date: Sat, 5 Jun 2021 14:30:52 +0800 Subject: [PATCH] add cors --- Configurations/SimApiOptions.cs | 5 +++-- SimApiExtensions.cs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Configurations/SimApiOptions.cs b/Configurations/SimApiOptions.cs index 67b2a69..6974bc1 100644 --- a/Configurations/SimApiOptions.cs +++ b/Configurations/SimApiOptions.cs @@ -4,14 +4,15 @@ namespace SimApi.Configs { public class SimApiOptions { + public bool EnableCors { get; set; } = true; public bool EnableSimApiAuth { get; set; } = false; public bool EnableSimApiDoc { get; set; } = true; public bool EnableSimApiException { get; set; } = true; public bool EnableSimApiStorage { get; set; } = false; public bool EnableForwardHeaders { get; set; } = true; public bool EnableLowerUrl { get; set; } = true; - public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions(); - public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions(); + public SimApiDocOptions SimApiDocOptions { get; set; } = new(); + public SimApiStorageOptions SimApiStorageOptions { get; set; } = new(); public void ConfigureSimApiDoc(Action options = null) diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 2f1f7c3..3553150 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -27,6 +27,12 @@ namespace SimApi builder.AddScoped(); } + if (simApiOptions.EnableCors) + { + builder.AddCors(cors => cors.AddPolicy("any", + policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); })); + } + // 使用SimApiDoc if (simApiOptions.EnableSimApiDoc) { @@ -160,6 +166,12 @@ namespace SimApi builder.UseForwardedHeaders(); } + if (options.EnableCors) + { + SimApiUtil.Log("开始配置Cors全部允许..."); + builder.UseCors("any"); + } + if (options.EnableSimApiAuth) { SimApiUtil.Log("开始配置SimApiAuth...");