我已经看了几个文件在启用跨原产地请求和所以回答这里,但我仍然没有得到正确的。 还跟随安装的IIS CORS模块
我有一个。NET core api 3.1在AWS EC2 Windows Server 2016数据中心的IIS上运行,endpoint如https://6.13.21.111/api/user/authenticate。 我有一个angular 8前端应用程序运行在AWS Amplify url上,比如https://tests.d1nkxxfifp945dd.myapp.com。
CORS策略阻止了从起源“https://tests.d1nkxxfifp945dd.myapp.com”访问位于“https://6.13.21.111/api/user/authenticate”得XMLHttpRequest:对预飞行请求得响应未通过访问控制检查:请求得资源上不存在“Access-Control-Allow-Origin”标头。 zone.js:3372 POST https://6.13.21.111/api/users/authenticate net::err_failed
当我从http://localhost:4200/向endpoint发送请求时,也会遇到同样的问题。我已经设置了以下内容
启动cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddControllers();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
用户控制器
[Route("api/[controller]")]
[EnableCors("MyAllowSpecificOrigins")]
[ApiController]
public class UsersController : ControllerBase
{
private IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpPost("authenticate")]
public Task<ApiResponse> Authenticate([FromBody]UserRequest userRequest)
{
return _userService.Authenticate(userRequest);
}
}
尝试将“AllowAnyOrigin”更改为withOrigin(“您的amazon angular URL”),看看它是否能解决问题。