简体   繁体   English

CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:25776/api/Upload”处的 XMLHttpRequest

[英]Access to XMLHttpRequest at 'http://localhost:25776/api/Upload' from origin 'http://localhost:4200' has been blocked by CORS policy

I am running angular spa application at http://localhost:4200 and .net core 3 application at http://localhost:25667 .我在http://localhost:4200运行 angular spa 应用程序,在http://localhost:25667运行 .net core 3 应用程序。

When my angular SPA keeps getting following error message.当我的 Angular SPA 不断收到以下错误消息时。

Access to XMLHttpRequest at 'http://localhost:25776/api/Upload' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Even though I think my CORS are configured correctly.即使我认为我的 CORS 配置正确。

Here is my startup.cs file.这是我的startup.cs文件。

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy(
                    name: "CorsPolicy",
                     builder => builder.WithOrigins("http://localhost:4200/")
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()
                    .SetIsOriginAllowed((host) => true));
            });

            services.AddHttpContextAccessor();

            services.AddControllers().AddNewtonsoftJson();
        }

        // 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();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors("CorsPolicy");

 

            app.UseAuthorization();


            app.UseDefaultFiles();
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Upload")),
                RequestPath = new PathString("/Upload")
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapFallbackToController("Index", "Fallback");
            });
        }

and here is my controller file这是我的控制器文件

using System.Collections.Generic;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TestApp.API.Models;
using TestApp.API.Services;
namespace TestApp.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [EnableCors("CorsPolicy")]
    public class UploadController : ControllerBase
    {
        // GET api/value
        [HttpGet]
        public IEnumerable<Upload> GetTempUploadData()
        {
            UploadServices uploadServices = new UploadServices();
            return uploadServices.GetTempUploadData();

        }
    }
}

I also have Microsoft.AspNetCore.Cors nuget package installed.我还安装了Microsoft.AspNetCore.Cors nuget 包。

Any help would be highly appreciated.任何帮助将不胜感激。

Thank you in advance.先感谢您。

You need to remvoe this / in WithOrigins .您需要remvoe本/WithOrigins

services.AddCors(options =>
        {
            options.AddPolicy(
                name: "CorsPolicy",
                 builder => builder.WithOrigins("http://localhost:4200")
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
                .SetIsOriginAllowed((host) => true));
        });

I am posting my solution, in case someone run into similar issue.我正在发布我的解决方案,以防有人遇到类似问题。

Issue was with launchSetting.json file within my API.问题在于我的 API 中的launchSetting.json文件。 I set anonymousAuthentication to true and it solved the issue.我将anonymousAuthentication设置为true并解决了这个问题。 It had nothing to do with CORS setting.它与 CORS 设置无关。

  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:25776/",
      "sslPort": 0
    }

暂无
暂无

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

相关问题 从源“http://localhost:4200”访问 XMLHttpRequest 已被 CORS 策略(Angular,WebApi)阻止 - Access to XMLHttpRequest from origin 'http://localhost:4200' has been blocked by CORS policy (Angular, WebApi) CORS 策略已阻止从源“http://localhost:4200”访问“URL”处的 XMLHttpRequest: - Access to XMLHttpRequest at 'URL' from origin 'http://localhost:4200' has been blocked by CORS policy: 来源 &#39;http://localhost:4200&#39; 已被 CORS 策略阻止 - origin 'http://localhost:4200' has been blocked by CORS policy 从CORS策略阻止从原始位置访问“ http:// localhost…”处的XMLHttpRequest: - Access to XMLHttpRequest at 'http://localhost…' from origin has been blocked by CORS policy: CORS 策略已阻止从源“http://localhost:3000”访问“https://localhost:7144/api/employees” - Access to fetch at 'https://localhost:7144/api/employees' from origin 'http://localhost:3000' has been blocked by CORS policy CORS 策略已阻止从源“http://**”访问“https://saja.smjd.ir/api/Account/login”处的 XMLHttpRequest - Access to XMLHttpRequest at 'https://saja.smjd.ir/api/Account/login' from origin 'http://**' has been blocked by CORS policy CORS 策略 .net 6 已阻止从源“LOCALHOST”访问“API URL”获取 - Access to fetch at 'API URL' from origin 'LOCALHOST' has been blocked by CORS policy .net 6 从源访问 xmlhttprequest 已被 Angular 6 中的 cors 策略阻止 - Access to xmlhttprequest at from origin has been blocked by cors policy in Angular 6 来自来源&#39;http:// localhost:&#39;的localhost:/ token请求已被CORS阻止 - Request at localhost:/token from origin 'http://localhost:' has been blocked by CORS “http://localhost:3000”已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查: - 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM