[英]Access-Control-Allow-Origin Problem in .NET Core Deployed at IIS Configured on Windows Server 2016
我已經在此處的Enable Cross-Origin Requests and SO Answers中瀏覽了幾個文檔,但我仍然沒有做對。 還安裝了 IIS CORS 模塊
I have a .net core api 3.1 running on IIS at AWS EC2 Windows Server 2016 datacenter, with endpoint like https://6.13.21.111/api/user/authenticate . I have an angular 8 front end app running on AWS Amplify url like https://tests.d1nkxxfifp945dd.myapp.com Any time I make a request to the API I get the error below.
Access to XMLHttpRequest at ' https://6.13.21.111/api/user/authenticate ' from origin ' https://tests.d1nkxxfifp945dd.myapp.com ' has been blocked by CORS policy: Response to preflight request doesn't pass access控制檢查:請求的資源上不存在“Access-Control-Allow-Origin”header。 zone.js:3372 POST https://6.13.21.111/api/users/authenticate net::ERR_FAILED
當我從http://localhost:4200/向端點發送請求時也遇到同樣的問題我已經設置了以下
啟動.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();
});
}
}
用戶 Controller
[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(“your amazon angular url”),看看是否能解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.