Cuture.AspNetCore.DynamicCors 1.0.0

dotnet add package Cuture.AspNetCore.DynamicCors --version 1.0.0
                    
NuGet\Install-Package Cuture.AspNetCore.DynamicCors -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cuture.AspNetCore.DynamicCors" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cuture.AspNetCore.DynamicCors" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Cuture.AspNetCore.DynamicCors" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cuture.AspNetCore.DynamicCors --version 1.0.0
                    
#r "nuget: Cuture.AspNetCore.DynamicCors, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Cuture.AspNetCore.DynamicCors@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Cuture.AspNetCore.DynamicCors&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Cuture.AspNetCore.DynamicCors&version=1.0.0
                    
Install as a Cake Tool

Cuture.AspNetCore.DynamicCors

A dynamic Cors support library based on Microsoft.AspNetCore.Cors. 基于 Microsoft.AspNetCore.Cors动态Cors 支持库。

1. Feature

  • 运行时动态调整允许跨域的域名列表
  • 基于 Microsoft.AspNetCore.Cors 工作,通过代理原始 ICorsPolicyProvider 实现功能,作为其补充
  • 支持泛域名配置
  • 简化允许源的重新加载

Note

与原始的 Microsoft.AspNetCore.Cors 相比,当前存在如下差异

  • 不验证端口号
  • 不验证协议

2. 快速开始

安装包

<PackageReference Include="Cuture.AspNetCore.DynamicCors" Version="1.0.0-*" />

添加服务

services.AddDynamicCors(builder =>
        {
            builder.AddDefaultPolicy(policyBuilder =>
            {
                //注意不允许配置 Origin
                policyBuilder.AllowAnyMethod()
                             .AllowCredentials()
                             .AllowAnyHeader();
            })
            //.SyncAllowedOriginsFromConfiguration("Cors:AllowedOrigins") //从 IConfiguration 同步允许跨域的域名列表
            .SyncAllowedOriginsWithOptions<SampleCorsOptions>(m => m.AllowedOrigins)  //从 IOptions<SampleCorsOptions> 同步允许跨域的域名列表
            .AddAllowedOriginsSyncSource<SampleAllowedOriginsSyncSource>() //添加从指定的同步源同步允许跨域的域名列表
            ;
        });

启用Cors

app.UseCors();

3. 自定义

3.1 自定义允许源的动态变更

使用 IDomainNameCollectionContainer.Get 方法获取指定策略的域名集合进行操作

var collection = collectionContainer.Get("policyName"); //获取指定策略名的域名集合
collection.Add("http://sample.com");  //添加指定域名
collection.Remove("http://sample.com"); //从集合中移除指定域名
collection.Reset(["http://sample.com", "http://sample2.com"]); //重置为指定集合
collection.Contains("http://sample.com"); //检查是否包含指定域名
collection.Clear(); //清空集合

3.2 实现允许来源同步源 - IAllowedOriginsSyncSource

允许来源同步源 为动态更新Cors域名集合提供支持。已实现抽象基类 AllowedOriginsSyncSource ,从其派生可以简化实现逻辑

class SampleAllowedOriginsSyncSource
    : AllowedOriginsSyncSource
{
    // PolicyName policyName 会自动注入策略名称
    public SampleAllowedOriginsSyncSource(PolicyName policyName, ILogger<SampleAllowedOriginsSyncSource> logger) : base(policyName)
    {
        //通知当前源已变更,程序会自动调用 GetOrigins 再次获取
        //NotifySourceChanged();
    }

    protected override Task<IEnumerable<string>> InnerGetOriginsAsync(CancellationToken cancellationToken)
    {
        //获取允许来源列表
        throw new NotImplementedException();
    }
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 354 11/12/2025
1.0.0-preview-003 805 11/5/2025
1.0.0-preview-002 5,098 9/15/2025
1.0.0-preview-001 196 9/14/2025