C# CORS不使用.NET核心API和Angular前端
我已经通读了类似的问题,尝试了其中提到的几个解决方案,但没有任何成功-问题很简单,我真的不知道会有什么错误 我有一个非常简单的.NET核心API,我想在它上面设置CORS 我尝试过这种方式(端点路由): Startup.cs中的ConfigureServices()和Configure()方法C# CORS不使用.NET核心API和Angular前端,c#,asp.net-core,cors,C#,Asp.net Core,Cors,我已经通读了类似的问题,尝试了其中提到的几个解决方案,但没有任何成功-问题很简单,我真的不知道会有什么错误 我有一个非常简单的.NET核心API,我想在它上面设置CORS 我尝试过这种方式(端点路由): Startup.cs中的ConfigureServices()和Configure()方法 [Route("[controller]/[action]")] public class SimplexSolverController : Controller
[Route("[controller]/[action]")]
public class SimplexSolverController : Controller
{
public IActionResult Ping()
{
return Json(new { status = "OK" });
}
[HttpPost]
public IActionResult Solve([FromBody] LPModelDto lpModelDto)
{
bool wrongFormat = false;
string message = null;
SimplexSolutionDto solution = null;
//...
}
//...
}
这正是默认生成的默认代码,只是添加了MSDN中与CORS相关的代码
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: "Policy1",
builder =>
{
// I read the site url from appsettings.json but for now I want to keep the example as simple as I can
// var lpSolverFrontendUrl = Configuration.GetValue<string>("Cors:AllowedSite");
builder.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod();
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers().RequireCors("Policy1");
});
}
我已经尝试过的另一种方法(使用属性):
Startup.cs这样做
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: "Policy1",
builder =>
{
// I read the site url from appsettings.json but I want to keep the example as simple as I can
// var lpSolverFrontendUrl = Configuration.GetValue<string>("Cors:AllowedSite");
builder.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod();
});
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
无论我选择哪种方式,我都会在DevTools控制台中得到一个错误,比如:在http://localhost:4000/simplexsolver/solve“起源”http://localhost:4200'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头
我完全按照文章所说的去做,但没有任何结果。。。谢谢你的帮助
更新
我终于成功地启用了CORS。遗憾的是,我无法在ConfigureServices中设置任何类型的策略,因为如果我这样做,则飞行前请求的响应中的Access Control Allow Origin标头将有一个“*”值(而不是前端的地址)。我使用的方式是:
- 在ConfigureServices()方法中,仅添加COR而不添加任何策略
- 在Configure()中,我将余量确定为UseCors()方法的参数 (不需要通过这种方式将EnableCors属性放在控制器mnethod上。)
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(options => options.WithOrigins(Configuration.GetValue<string>("Cors:AllowedSite")).AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
public void配置服务(IServiceCollection服务)
{
services.AddCors();
services.AddControllers();
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(options=>options.WithOrigins(Configuration.GetValue(“Cors:AllowedSite”)).AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
});
}
谢谢你给我的所有答案!我希望这将有助于未来的人。但我仍然很奇怪,在策略中使用CORS有什么问题。请将您的代码修改为:
app.UseCors("Policy1");
services.AddCors(options =>
{
options.AddPolicy("Policy1",
builder =>
{
.builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
移除
[EnableCors("Policy1")]
通过控制器操作。我能够在.NET Core 3.1中全局允许CORS: Startup.cs代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy(name: "AllowCORS", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("AllowCORS");
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
使用以下命令:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin", builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
//...
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin", builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
//...
}
如果不起作用,请使用将Mvc兼容版本设置为3.0
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
像这样:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin", builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
//...
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowOrigin", builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
//...
}
有趣。但是调试器显示了什么,是一个飞行前请求()发生吗?请考虑看这里。出于调试目的,您可能希望使用以下命令:
app.UseCors(options=>options.SetIsOriginAllowed(x=>=true).AllowAnyMethod().AllowAnyHeader().AllowCredentials());//这需要将所有内容设置为允许。
它是否工作。您好,飞行前请求由ofc浏览器发送。问题是Access-Control.Allow-Origin的值为“*”,而不是Angular站点的url。似乎可以通过在UseCors()cass中定义容差作为参数来解决:)请参阅更新