using Microsoft.Extensions.DependencyInjection;
namespace DS.Module.Core.ServiceExtensions;
///
/// 跨域服务
///
public static class CorsInstall
{
///
/// 将模块服务添加到依赖注入服务容器中
///
/// 依赖注入服务容器
///
public static IServiceCollection AddCorsInstall(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
//跨域第二种方法,声明策略,记得下边app中配置
//跨域请求
string policyName = AppSetting.Configuration["Cors:PolicyName"];
var corsUrl = AppSetting.Configuration["Cors:Url"];
services.AddCors(c =>
{
c.AddPolicy(policyName, policy =>
{
policy.WithOrigins(corsUrl
.Split(",", StringSplitOptions.RemoveEmptyEntries).ToArray())
//policy.WithOrigins("http://localhost:5001")//支持多个域名端口,注意端口号后不要带/斜杆:比如localhost:8000/,是错的
.AllowAnyHeader().AllowAnyMethod().AllowCredentials(); //允许cookie;
});
});
return services;
}
}