43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
|
|
using Microsoft.AspNetCore.Builder;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
|
|
|||
|
|
namespace QYZH.InteractiveMagazine.Infrastructure.Middleware
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 跨域扩展
|
|||
|
|
/// </summary>
|
|||
|
|
public static class CorsExtension
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 跨域配置
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="services"></param>
|
|||
|
|
/// <param name="configuration"></param>
|
|||
|
|
public static void AddCorsRegister(this WebApplicationBuilder builder)
|
|||
|
|
{
|
|||
|
|
bool isCorsAll = builder.Configuration["corsUrls"] == "*";
|
|||
|
|
builder.Services.AddCors(options =>
|
|||
|
|
{
|
|||
|
|
options.AddPolicy("defaultCors", policy =>
|
|||
|
|
{
|
|||
|
|
if (isCorsAll)
|
|||
|
|
{
|
|||
|
|
policy.SetIsOriginAllowed(origin => true);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
var corsUrls = builder.Configuration.GetSection("corsUrls").Get<string[]>();
|
|||
|
|
policy.WithOrigins(corsUrls ?? Array.Empty<string>());
|
|||
|
|
}
|
|||
|
|
policy.AllowAnyHeader()
|
|||
|
|
.AllowAnyMethod()
|
|||
|
|
.AllowCredentials()
|
|||
|
|
.WithExposedHeaders("X-New-Token");
|
|||
|
|
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|