简体   繁体   English

如何在 ASP.net Core WebAPI 中使用默认和自己的策略启用 CORS

[英]How to enable CORS in ASP.net Core WebAPI with deafult and own policy

I would like to enable by EnableCors attribute my own "MyPolicy" for one controller and for the others I would like to use default policy.我想通过 EnableCors 属性为一个 controller 启用我自己的“MyPolicy”,而其他我想使用默认策略。 So in my configure services method I write所以在我的配置服务方法中我写

services.AddCors(options =>
{
    options.AddPolicy(name: "MyPolicy",
        builder => builder
            .WithOrigins("http://localhost:3000")
            .AllowCredentials()
            .AllowAnyMethod()
            .AllowAnyHeader());

    options.AddDefaultPolicy(
            builder => builder
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader());
});

and than in Configure method I just call:而不是在配置方法中我只是调用:

app.UseCors();

it does not work as I expected.它不像我预期的那样工作。 It's only define DefaultPolicy and the only way to use "MyPolicy" is to use them as:它只定义 DefaultPolicy,使用“MyPolicy”的唯一方法是将它们用作:

app.UseCors("MyPolicy");

But in this case default policy does not work.但在这种情况下,默认策略不起作用。 Is it possible to define own policies by AddPolicy and default policy by AddDefaultPolicy.是否可以通过 AddPolicy 定义自己的策略,并通过 AddDefaultPolicy 定义默认策略。

If you would like to use many own policies and default policy the solution is to define in configureservices:如果您想使用许多自己的策略和默认策略,解决方案是在 configureservices 中定义:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddDefaultPolicy(
            builder =>
            {
               
                builder.WithOrigins("http://example.com",
                                    "http://www.contoso.com");
            });

        options.AddPolicy("AnotherPolicy",
            builder =>
            {
                builder.WithOrigins("http://www.contoso.com")
                                    .AllowAnyHeader()
                                    .AllowAnyMethod();
            });

    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

and use policies through EnableCorsAttribute like this:并通过 EnableCorsAttribute 使用策略,如下所示:

  // GET api/values
    [EnableCors("AnotherPolicy")]
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "green widget", "red widget" };
    }

. . In this case do not call UseCors method of app IApplicationBuilder object in configure method startup class.在这种情况下,不要在配置方法启动 class 中调用应用程序 IApplicationBuilder object 的 UseCors 方法。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }
    //Do not use this method:
    //app.UseCors();

    app.UseHttpsRedirection();
    app.UseMvc();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM