繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM