forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCorsSetup.cs
More file actions
37 lines (34 loc) · 1.31 KB
/
CorsSetup.cs
File metadata and controls
37 lines (34 loc) · 1.31 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
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 =>
{
c.AddPolicy("LimitRequests", policy =>
{
// 支持多个域名端口,注意端口号后不要带/斜杆:比如localhost:8000/,是错的
// 注意,http://127.0.0.1:1818 和 http://localhost:1818 是不一样的,尽量写两个
policy
.WithOrigins(Appsettings.app(new string[] { "Startup", "Cors", "IPs" }).Split(','))
.AllowAnyHeader()//Ensures that the policy allows any header.
.AllowAnyMethod();
});
// 允许任意跨域请求,也要配置中间件
//c.AddPolicy("AllRequests",policy=> {
// policy.AllowAnyOrigin();
// policy.AllowAnyMethod();
// policy.AllowAnyHeader();
//});
});
}
}
}