【问题标题】:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource?跨域请求被阻止:同源策略不允许读取远程资源?
【发布时间】:2021-04-04 12:12:51
【问题描述】:

我使用 file system 在 IIS 上发布我的 .net 核心应用程序,当我在浏览器中打开应用程序时,它不会连接到数据库并将我抛出错误(在 firefox 的控制台中),但如果我从 VS 2019 运行它可以工作很好,没有任何错误。非常感谢任何帮助。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:44374/api/userLogin/?username=test&password=test. (Reason: CORS request did not succeed)

这是我的appsetting

  "Logging": {
      "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
      }
    },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "patientDB": "Data Source=DESKTOP-FE7Q19U\\SQLEXPRESS;Initial Catalog=MDSS;Integrated Security=True"
  }
}

这是我的launchsetting

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51911",
      "sslPort": 44374
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "ReactC_MDSS": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

这是我的startup

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllersWithViews();
            services.AddDbContext<patientDbContext>(o => o.UseSqlServer(Configuration.GetConnectionString("patientDB")));
           
            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/build";
            });


   

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseReactDevelopmentServer(npmScript: "start");
                }
            });
        }
    }
}

我尝试添加 cors 但不知何故它不起作用。

【问题讨论】:

  • 您可以包含来自同一页面的两个或多个请求。当浏览器请求服务器时,它会检查它是否来自同一来源。如果它来自不同的来源会导致这种情况。另一个原因是浏览器可能有安全策略,因此请求被阻止。

标签: asp.net asp.net-core


【解决方案1】:

IIS 上的 cors-error 与您开发时使用的端口相同 (https://localhost:44374)。你能在你的反应代码中显示 API 请求的代码吗?你真的指的是应用程序基地址(即'api/userLogin/?username=test&password=test')还是硬编码的本地主机路径('https://localhost:44374/api/userLogin/?username=test&password=test ')?由于 CORS 策略,后者将被浏览器阻止。但是可以在 Startup.cs 中添加异常

【讨论】:

  • 如果浏览器在 https://localhost:44374/something 上,那么对 https://localhost:44374/api/userLogin 的请求不应触发 CORS,无论您是否使用主机相对 URL (/api/userLogin)。跨度>
  • 我正在使用这个 const customerApi = 'localhost:44374/api/userLogin/?username=' + document.getElementById("txtemail").value + '&password=' + document.getElementById("txtpassword").value;跨度>
  • @Andreas Sundström 你能告诉我把异常放在哪里吗?
  • 好的,所以尝试改用const customerApi = 'api/userLogin/?username... 之类的东西。请参阅documentation 了解如何允许 CORS。 @poke 不,这是真的,但我的猜测是他没有将他的 IIS 配置为在端口 44374 上运行。端口 80 或 443 更常见(默认情况下)。如果他确实在端口 80/443 上运行了它,则对 localhost:44374 的请求将被浏览器的 CORS 阻止。
猜你喜欢
  • 2014-10-17
  • 2015-07-22
  • 2018-04-20
  • 2014-07-20
  • 1970-01-01
  • 2019-07-12
相关资源
最近更新 更多