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.

48 lines
1.6 KiB
C#

11 months ago
using Admin.Core.Common;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Admin.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();
});
}
});
}
}
}