c# - 使用 .NET Core 和 Angular 6 启用 CORS

标签 c# asp.net-core angular6

我正在尝试在 ASP.NET Core API 中启用 CORS,并允许从我的客户端应用程序 (Angular 6) 传递 cookie。但是,每当我尝试通过 Angular 应用程序访问端点时,我都会收到以下错误:

Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'

在我的 Startup.cs 文件中,我在 ConfigureServices 下启用了 CORS,例如:

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

并在“配置”下:

app.UseCors("CorsPolicy");

在我的 Angular 6 应用程序中,我使用如下 token 调用端点:

this.http.get<T>(url, { headers: new HttpHeaders({ 'X-XSRF-TOKEN': token}), withCredentials: true });

该错误令人困惑,因为我在 .AddCors 函数中明确设置了 .WithOrigins() 中允许的来源,但它仍然说只有一个通配符。

最佳答案

如果它托管在 Azure 上。检查 Azure AppService 中的 CORS 设置。即使在 .net core 中间件中,那里的配置也将覆盖任何其他 Cors 配置。

关于c# - 使用 .NET Core 和 Angular 6 启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52545698/

相关文章:

iis - 每个路由有不同的身份验证模式(Windows、Bearer)

c# - 使用 TestServer 在 AspNetCore 中的集成测试中模拟和解决 Autofac 依赖性

Angular 迁移(从 4 到 6)e2e --proxy-config 不工作

angular - 使用 Angular 6 中的 Reactive 形式的递归形式( TreeView )

c# - 我的 IEnumerator 正在工作但没有等待

C# 我在变量未按预期运行时遇到问题

c# - 如果表单没有焦点,则不会调用 OnPaint

javascript - 如何使用带有 Razor 的 MVC4 设置 javascript 变量

angular6 - 如何在 JHipster 项目中渲染没有 html 标签的 ngx-quill-editor 内容?

c# - 使用 C# 和 Magentostore 上传和下载产品