角.net核心api cors预检请求错误

时间:2018-11-07 16:03:41

标签: angular .net-core

我无法使cors变为.net core 2.1,但出现此错误:

从原点“ https://dev...SaveAPP”到“ https://page”处对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上。

1 个答案:

答案 0 :(得分:1)

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

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

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

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

安装cors-proxy-server

#include <stdio.h>
#include <string.h>

#define N 21

int read_line(char smallest[], char largest[]);

int not_first = 0;

int main(){
  char smallest[N], largest[N];
  int check = 0;

  while(check != 4){
    check = read_line(smallest, largest);
  }

  printf("Smallest word: %s\n", smallest);
  printf("Largest word: %s\n", largest);

  return 0;
}

int read_line(char smallest[], char largest[]){
  char input[N];

  printf("Enter word: ");
  scanf("%s", input);

  if(strcmp(input, smallest) < 0 || !not_first){
    printf("Smallest is: %s was: %s\n", input, (not_first)? smallest : "(none)");
    strcpy(smallest, input);
  }
  if(strcmp(input, largest) > 0 || !not_first){
    printf("Largest is: %s was: %s\n", input, (not_first)? largest : "(none)");
    strcpy(largest, input);
  }
  not_first = 1;

  return strlen(input);
}

启动

/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

编辑heroes.service以更改URL以包括代理

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

保存更改并成功!!!!

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

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

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

相关问题