首页 > 解决方案 > angular .net core api cors 预检请求错误

问题描述

我无法使 cors 工作 angular .net core 2.1 我收到此错误:

从源“ https://page ”访问“ https://dev...SaveAPP ”的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:预检不允许重定向要求。

这是我的startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AllowAll",
            builder => builder.WithOrigins("http://localhost:4200", "https://e.corpintra.net", "https://specit-dtna-dev.e.corpintra.net", "https://specit-dtna.e.corpintra.net", "https://specit-dtna-test.e.corpintra.net")
                                  .AllowAnyMethod()
                                  .WithExposedHeaders("content-disposition")
                                  .AllowAnyHeader()
                                  .AllowCredentials()
                                  .SetPreflightMaxAge(TimeSpan.FromSeconds(3600)));
    });

    services.Configure<MvcOptions>(options =>
    {
        options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAll"));
    });

    services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    if (env.IsProduction() || env.IsStaging())
    {
        app.UseExceptionHandler("/Error");
    }
    // app.UseCorsMiddleware();
    app.UseCors("AllowAll");
    //    app.UseCors(builder =>
    //          builder.WithOrigins("http://localhost:4200", "http://localhost:5000")
    //.AllowAnyOrigin()
    //.AllowAnyHeader()
    //.AllowAnyMethod());

    app.UseMvc();
}

从角度我正在使用拦截器

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    request = request.clone({
      withCredentials: true
    });
    return next.handle(request);
}

我的 api 同时启用了 Windows 身份验证和匿名身份验证。

这是在 Intranet 上。

标签: angular.net-core

解决方案


(已解决)在尝试将 Tour of Heroes Angular 7 Demo 转换为对远程 MySQL 数据库进行 REST 调用时,我遇到了类似的错误。我什至尝试将 Apache 服务器移动到我的本地计算机:

从源“ http://localhost:4200 ”访问“ http://localhost/angular-php-app/backend ”处的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:重定向不允许用于预检请求。

解决方案,我在这里找到了 -> http://blog.wsoft.se/category/tips-tricks/

这是我在 Angular 应用程序中所做的:

安装 cors-proxy-server

/tour-of-heroes-mysql>npm install -g cors-proxy-server

...Roaming\npm\cors-proxy-server -> ...Roaming\npm\node_modules\cors-proxy-server\index.js
+ cors-proxy-server@1.0.2
added 8 packages from 10 contributors in 2.376s

启动它

tour-of-heroes-mysql>HOST=127.0.0.1 PORT=9090 cors-proxy-server &
[1] 17172
/tour-of-heroes-mysql>[Sun Jan 27 2019 10:52:13 GMT-0500 (Eastern Standard Time)] - CORS Proxy Server started on 127.0.0.1:9090

编辑 heros.service 以更改 URL 以包含代理

    private heroesUrl = 'http://localhost/angular-php-app/backend';  // URL to web api
-->  private heroesUrl = 'http://localhost:9090/http://localhost/angular-php-app/backend'

保存了更改,它工作了!!!!

注意:“PUT”操作有一些问题,不确定代理是否正确处理了这些问题。使用 CHROME 发现这是一个更好的解决方案,只需通过调整目标上的 Windows 属性来禁用检查:

 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="C:/ChromeDevSession"

(注意 - 当您启动 CHROME 时,您会收到“不支持”警告,但它可以工作。在您的桌面上创建一个特殊的快捷方式,仅用于测试)


推荐阅读