我正在尝试在我的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?


当前回答

这涵盖每个端点。如果你想阻止某个端点,使用这个注释[DisableCors] 这里描述得很好。 https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0

在app.authentication()和app.routing()之间添加app.usecors(policyName)在Configure方法中。 在configureService方法中

services.AddCors(options => options.AddPolicy(name: mypolicy,     builder =>     { builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); }));

在每个控制器中添加[EnableCors("mypolicy")]

        [EnableCors("mypolicy")] 
        [Route("api/[controller]")] [ApiController] 
        public class MyController : ControllerBase


eg:-

  namespace CompanyApi2
        {
            public class Startup
            {
                public Startup(IConfiguration configuration)
                {
                    Configuration = configuration;
                }
        
                public IConfiguration Configuration { get; }
        
                // This method gets called by the runtime. Use this //method to add services to the container.
                public void ConfigureServices(IServiceCollection services)
                {
                    services.AddCors(options =>
                        options.AddPolicy(name: mypolicy,
                            builder =>
                            {
                                builder.AllowAnyHeader().AllowAnyMethod()
                                    .AllowAnyOrigin();
                            })); //add this
                    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
                    services.AddScoped<IDatarepository, DatabaseRepository>();
                }
        
                public string mypolicy = "mypolicy";
        
                // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
                public void Configure(IApplicationBuilder app, IHostingEnvironment env)
                {
                    if (env.IsDevelopment())
                    {
                        app.UseDeveloperExceptionPage();
                    }
                    else
                    {
                        app.UseHsts();
                    }
        
                    app.UseCors(mypolicy); //add this
                    app.UseHttpsRedirection();
                    app.UseMvc();
                }
            }
        }

其他回答

最近,当在azure web应用程序上托管web应用程序服务器时,不得不编辑azure应用程序cors设置来解决这个问题(只有代码没有解决这个问题)

不太安全-启用访问控制-允许-凭据-留空,并添加*作为允许的原点 标记为启用访问控制-允许凭据,然后添加希望允许来自的请求的域

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

    services.Configure<MvcOptions>(options => {
        options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAnyOrigin"));
    });            
}

ASP。NET核心Web API

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

在“配置”中添加UseCors

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

.Net CORE 3.1使用:

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

步骤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方法(或您特定的控制器和动作):

更多详情请点击这里