【问题标题】:IIS Server hosting API and Angular 2 app No CORS sentIIS 服务器托管 API 和 Angular 2 应用程序未发送 CORS
【发布时间】:2017-08-30 01:42:25
【问题描述】:

TLDR:1 个服务器、IIS、两个站点、预检响应中缺少 CORS 标头

> http://myserver:88   // This is my angular site
> http://myserver:8888 // This is my .NET CORE API

背景:

我开发了一个 ASP.NET CORE API 和一个 Angular Web 应用程序。 我将它们与 IIS 托管在同一台服务器上。它们是两个不同的“站点”。

我遇到了 CORS 问题。我像这样强制发送响应头......

现在在我的登录端点中,我需要自定义标头。所以需要一个预检响应.. chrome显示了这个..

然后下一个电话显示了这一点。

我不确定我在这里做错了什么......

这就是我的 cors 设置在 API 中的样子。

public void ConfigureServices(IServiceCollection services)
    {
        string[] origins = new [] { "http://myServer:88" }; // PORT TO HOSTED ANGULAR SITE

        services.AddCors(options => {
            options.AddPolicy("AllowSpecificOrigin", builder =>
                builder.WithOrigins(origins)
                    .WithMethods("GET", "POST")
                    .WithHeaders("accept", "content-type", "origin", "username", "password")
                    .AllowCredentials()
                );
            });

        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
app.UseCors("AllowSpecificOrigin");

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

【问题讨论】:

  • 我将端点包裹在 try catch 和 catch 返回的异常 e 中。数据库连接不正确并引发错误。这个异常以某种方式触发了 CORS 错误。

标签: iis asp.net-core-webapi


【解决方案1】:

我的 CORS 设置没有任何问题。这是由于在我的端点中引发了异常。我希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多