我正在尝试在我的ASP上启用跨起源资源共享。NET核心Web API,但我卡住了。

EnableCors属性接受字符串类型的policyName作为参数:

// Summary:
//     Creates a new instance of the Microsoft.AspNetCore.Cors.Core.EnableCorsAttribute.
//
// Parameters:
//   policyName:
//     The name of the policy to be applied.
public EnableCorsAttribute(string policyName);

policyName是什么意思,如何在ASP上配置CORS。NET核心Web API?


当前回答

根据Henk的回答,我已经能够提出特定的域,我想允许的方法,以及我想启用CORS的头:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
         options.AddPolicy("AllowSpecific", p => p.WithOrigins("http://localhost:1233")
                                                   .WithMethods("GET")
                                                   .WithHeaders("name")));
    services.AddMvc();
}

用法:

[EnableCors("AllowSpecific")]

其他回答

适用于。net Core 1和。net Core 2

如果使用.Net-Core 1.1

不幸的是,在这个特定的情况下,文件非常混乱。所以我要让它变得非常简单:

将Microsoft.AspNetCore.Cors nuget包添加到项目中 在ConfigureServices方法中添加services.AddCors(); 在Configure方法中,在调用app.UseMvc()和app.UseStaticFiles()之前,添加: app.UseCors(生成器=>生成器 .AllowAnyOrigin () .AllowAnyMethod () .AllowAnyHeader () .AllowCredentials ());

就是这样。每个客户端都可以访问您的ASP。NET核心网站/API。


如果使用。net - core 2.0

Add Microsoft.AspNetCore.Cors nuget package to your project in ConfigureServices method, before calling services.AddMvc(), add: services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); (Important) In Configure method, before calling app.UseMvc(), add app.UseCors("AllowAll"); "AllowAll" is the policy name which we need to mention in app.UseCors. It could be any name.

根据Henk的回答,我已经能够提出特定的域,我想允许的方法,以及我想启用CORS的头:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
         options.AddPolicy("AllowSpecific", p => p.WithOrigins("http://localhost:1233")
                                                   .WithMethods("GET")
                                                   .WithHeaders("name")));
    services.AddMvc();
}

用法:

[EnableCors("AllowSpecific")]

步骤1:我们需要微软. aspnetcore . cors包在我们的项目。安装请进入“Tools -> NuGet Package Manager -> Manage NuGet Packages For Solution”。搜索“Microsoft.AspNetCore.Cors”,安装软件包。

步骤2:我们需要将CORS注入到容器中,以便应用程序可以使用它。在Startup.cs类中,让我们转到ConfigureServices方法并注册CORS。

因此,在我们的服务器应用程序中,让我们进入Controllers -> homeconcontroller .cs,并将EnableCors装饰器添加到Index方法(或您特定的控制器和动作):

更多详情请点击这里

你有三种方式启用CORS:

在中间件中使用命名策略或默认策略。 使用端点路由。 使用[EnableCors]属性。

启用指定策略的CORS:

public class Startup
{
    readonly string CorsPolicy = "_corsPolicy";

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(name: CorsPolicy,
                              builder =>
                              {
                                 builder.AllowAnyOrigin()
                                      .AllowAnyMethod()
                                      .AllowAnyHeader()
                                      .AllowCredentials();
                              });
        });

        // services.AddResponseCaching();
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();

        app.UseCors(CorsPolicy);

        // app.UseResponseCaching();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

当使用UseResponseCaching时,UseCors必须在UseResponseCaching之前调用。

开启默认策略下的CORS:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddDefaultPolicy(
                builder =>
                {
                     builder.AllowAnyOrigin()
                                      .AllowAnyMethod()
                                      .AllowAnyHeader()
                                      .AllowCredentials();
                });
        });

        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();

        app.UseCors();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

启用带有端点的CORS

public class Startup
{
    readonly string CorsPolicy = "_corsPolicy ";

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(name: CorsPolicy,
                              builder =>
                              {
                                  builder.AllowAnyOrigin()
                                      .AllowAnyMethod()
                                      .AllowAnyHeader()
                                      .AllowCredentials();
                              });
        });

        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();

        app.UseCors();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers()
                     .RequireCors(CorsPolicy)
        });
    }
}

启用带有属性的CORS

你有两个选择

[EnableCors]默认策略。 [EnableCors("{Policy String}")]指定命名策略。

ASP。NET核心Web API

在ConfigureServices中添加services.AddCors();之前services.AddControllers ();

在“配置”中添加UseCors

app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());