【问题标题】:angular .net core api cors preflight request errorangular .net core api cors 预检请求错误
【发布时间】:2019-04-11 02:33:33
【问题描述】:

我无法让 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


    【解决方案1】:

    (已解决)我在尝试将 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 时,您会收到“不支持”警告,但它可以工作。在您的桌面上创建一个特殊的快捷方式,仅用于测试)

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 2020-10-26
      • 2021-08-26
      • 2022-09-30
      • 2020-11-18
      • 2017-05-06
      • 2018-11-28
      • 2020-03-14
      • 2019-03-10
      相关资源
      最近更新 更多