username@email.com
2024-07-02 89879d47da4c63103ec38595c5dd014a12c01cca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/***********************************************************************
 *            Project: baifenBinfa.Net                                     *
 *                Web: https://baifenBinfa.com                             *
 *        ProjectName: 百分兵法管理系统                               *
 *             Author:                                        *
 *              Email:                               *
 *           Versions: 1.0                                             *
 *         CreateTime: 2020-02-03 22:49:41
 *          NameSpace: CoreCms.Net.Framework
 *           FileName: Cors
 *   ClassDescription:
 ***********************************************************************/
 
 
using System;
using CoreCms.Net.Configuration;
using CoreCms.Net.Utility.Extensions;
using Microsoft.Extensions.DependencyInjection;
 
namespace CoreCms.Net.Core.Config
{
    /// <summary>
    /// 配置跨域(CORS)
    /// </summary>
    public static class CorsSetup
    {
        public static void AddCorsSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            services.AddCors(c =>
            {
                if (!AppSettingsConstVars.CorsEnableAllIPs)
                {
                    c.AddPolicy(AppSettingsConstVars.CorsPolicyName, policy =>
                        {
                            policy.WithOrigins(AppSettingsConstVars.CorsIPs.Split(','));
                            //policy.AllowAnyOrigin();
                            policy.AllowAnyHeader();
                            policy.AllowAnyMethod();
                            policy.AllowCredentials();
                        });
                }
                else
                {
                    //允许任意跨域请求
                    c.AddPolicy(AppSettingsConstVars.CorsPolicyName, policy =>
                        {
                            policy.SetIsOriginAllowed((host) => true)
                                .AllowAnyMethod()
                                .AllowAnyHeader()
                                .AllowCredentials();
                        });
                }
            });
        }
    }
}