简体   繁体   English

.NET 核心中的访问控制允许来源问题 部署在 IIS 配置在 Windows 服务器 2016

[英]Access-Control-Allow-Origin Problem in .NET Core Deployed at IIS Configured on Windows Server 2016

I have gone through several documentations at Enable Cross-Origin Requests and SO Answers here but I'm still not getting it right.我已经在此处的Enable Cross-Origin Requests and SO Answers中浏览了几个文档,但我仍然没有做对。 Also followed Installed IIS CORS module安装了 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 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. 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 control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 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 zone.js:3372 POST https://6.13.21.111/api/users/authenticate net::ERR_FAILED

Also getting the same issue when I send request to endpoint from http://localhost:4200/ I have setup the following当我从http://localhost:4200/向端点发送请求时也遇到同样的问题我已经设置了以下

Startup.cs启动.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();
        });
    }
}

User Controller用户 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);
    }

}

Try changing "AllowAnyOrigin"to withOrigin("your amazon angular url") and see whether it will resolve the problem.尝试将“AllowAnyOrigin”更改为 withOrigin(“your amazon angular url”),看看是否能解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 6 中的“访问控制允许来源” - 'Access-Control-Allow-Origin' in ASP.NET Core 6 Access-Control-Allow-Origin在IIS中,但不起作用 - Access-Control-Allow-Origin is in IIS, but not working 在Access-Control-Allow-Origin标头中找不到源[域]。 ASP .NET CORE API MVC - Origin [domain] not found in Access-Control-Allow-Origin header. ASP .net CORE API mvc ASP.NET Core CORS WebAPI:没有 Access-Control-Allow-Origin 标头 - ASP.NET Core CORS WebAPI: no Access-Control-Allow-Origin header .NET Core 中请求的资源上不存在“Access-Control-Allow-Origin”header - No 'Access-Control-Allow-Origin' header is present on the requested resource in .NET Core React+ASP.NET.Core:请求的资源上不存在“Access-Control-Allow-Origin”标头 - React+ASP.NET.Core : No 'Access-Control-Allow-Origin' header is present on the requested resource NET CORE 发送大型 JSON 时没有“访问控制允许来源” - NET CORE getting no 'Access-Control-Allow-Origin' when sending large JSON 在Asp.net Core 2.1的Angular 6中不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present Angular 6 with Asp.net Core 2.1 ASP.Net核心WebAPI - 请求的资源上没有“Access-Control-Allow-Origin”标头 - ASP.Net Core WebAPI - No 'Access-Control-Allow-Origin' header is present on the requested resource CORS asp.net 核心 webapi - 缺少 Access-Control-Allow-Origin 标头 - CORS asp.net core webapi - missing Access-Control-Allow-Origin header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM