forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorsSetup.cs
More file actions
48 lines (43 loc) · 1.58 KB
/
CorsSetup.cs
File metadata and controls
48 lines (43 loc) · 1.58 KB
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
using Blog.Core.Common;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Blog.Core.Extensions
{
/// <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 (!AppSettings.app(new string[] { "Startup", "Cors", "EnableAllIPs" }).ObjToBool())
{
c.AddPolicy(AppSettings.app(new string[] { "Startup", "Cors", "PolicyName" }),
policy =>
{
policy
.WithOrigins(AppSettings.app(new string[] { "Startup", "Cors", "IPs" }).Split(','))
.AllowAnyHeader()//Ensures that the policy allows any header.
.AllowAnyMethod();
});
}
else
{
//允许任意跨域请求
c.AddPolicy(AppSettings.app(new string[] { "Startup", "Cors", "PolicyName" }),
policy =>
{
policy
.SetIsOriginAllowed((host) => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
}
});
}
}
}