angular .net core api cors预检请求错误

标签 angular .net-core

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

在“https://dev...SaveAPP”访问 XMLHttpRequest '来自原产地' https://page '已被 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();
}

从 Angular 来看我正在使用拦截器

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

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

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

这是在 Intranet 上。

最佳答案

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

在“http://localhost/angular-php-app/backend”访问 XMLHttpRequest '来自原产地' http://localhost:4200 '已被 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

编辑 heroes.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 时,您会收到“不受支持”的警告,但它有效。在您的桌面上创建一个特殊的快捷方式,仅用于测试)

关于angular .net core api cors预检请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193256/

相关文章:

带有字符串变量的 Angular 2 NgTemplateOutlet。

html - Angular 中的羽毛笔编辑器 (editor.root.innerHTML)

asp.net - ASP .Net Core 在 JWTBearerOptions.Events OnTokenValidated 事件中访问 DistributedRedisCache

c# - 带有 .Net Core 2.1 的 HttpClient 挂起

javascript - Angular 2 RC 5 Internet Explorer 10 滚动性能不佳

angular - 更新 Clarity 种子项目

vb.net - 是否可以在 VB.NET 中在 Raspbian 上运行服务?

c# - 计时器以秒为单位精确计时

asp.net - 当前不会命中断点 - 没有可执行代码与此行相关联

angular - 如何使用 canActivate 保护 Angular CLI Assets