C# CORS停止Angular应用程序和.NET CORE 3.0 API之间的通信
我尽量避免发布这个问题,因为还有很多类似的问题,但在尝试了似乎所有的组合之后,没有一个解决了我的问题 问题 当angular应用程序在IIS EXPRESS上本地运行时,它可以发布到web API。但是当API部署到IIS时,我的Angular应用程序无法发布到它。但是,它可以在两种环境中成功执行GET请求 错误C# CORS停止Angular应用程序和.NET CORE 3.0 API之间的通信,c#,asp.net-web-api,cors,asp.net-core-3.1,C#,Asp.net Web Api,Cors,Asp.net Core 3.1,我尽量避免发布这个问题,因为还有很多类似的问题,但在尝试了似乎所有的组合之后,没有一个解决了我的问题 问题 当angular应用程序在IIS EXPRESS上本地运行时,它可以发布到web API。但是当API部署到IIS时,我的Angular应用程序无法发布到它。但是,它可以在两种环境中成功执行GET请求 错误 [ApiController] [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/[contro
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class PostDataController : ControllerBase
{
...other stuff...
[HttpPost]
public async Task<SomeResponse> Post(SomeRequest sr)
{
...other stuff...
}
}
访问位于“”的XMLHttpRequesthttps://mySite:1234/api/v1/postData“起源”https://mySite:1233'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头
我所尝试的/代码
这两个应用程序都在不同的端口上运行,因此我理解了为什么需要配置CORS,在阅读了相当多的内容后,我发现没有针对GET请求()发出飞行前请求,因此这可以解释GET请求成功的原因。Mozilla文档让我相信我的帖子不属于简单请求类别,因为它需要授权,但这并不能回答它在IIS EXPRESS上的工作方式,而不是在IIS上的工作方式(我认为EXPRESS有点不那么严格?)
在遵循了这一原则,但无法使其发挥作用之后,我考虑了各种各样的选择,尝试了各种各样的选择,我已经走到了一条死胡同,我真的希望有人能指出我哪里出了问题
Startup.cs
public class Startup
{
private const string ALLOWED_ORIGINS = "allowedOrigin";
...other stuff...
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: ALLOWED_ORIGINS,
builder =>
{
builder.WithOrigins("https://myLocalSite:1233", "https://myDeployedSite:1233")//these values are retrieve from config
.AllowAnyMethod()
.AllowAnyHeader();
});
});
services.AddControllers()
....other stuff...
services.AddMvc(o =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
o.Filters.Add(new AuthorizeFilter(policy));
});
....other stuff...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...other stuff...
app.UseRouting();
app.UseCors(ALLOWED_ORIGINS);
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
...other stuff...
}
}
控制器
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class PostDataController : ControllerBase
{
...other stuff...
[HttpPost]
public async Task<SomeResponse> Post(SomeRequest sr)
{
...other stuff...
}
}
[ApiController]
[ApiVersion(“1.0”)]
[路由(“api/v{version:apiVersion}/[控制器]”)
公共类PostDataController:ControllerBase
{
…其他东西。。。
[HttpPost]
公共异步任务发布(SomeRequest sr)
{
…其他东西。。。
}
}
最后
我没有添加任何angular代码,因为我相信问题出在我的API设置中。如果需要,我很乐意添加代码
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
}
public void Configure(IApplicationBuilder app)
{
app.UseCors(policy => policy.WithOrigins(ALLOWED_ORIGINS));
}
公共类启动
{
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
options.AddPolicy(
“AllowAnysampleRequest”,
生成器=>
{
builder.AllowAnyOrigin()
.使用方法(“获取”、“发布”、“头”);
});
查看此文件以获得更好的“完整上下文”代码。错误消息包含mysite,它不在cors策略中。这是从传输到堆栈溢出的错误吗?这是错误,感谢您指出