cors - Azure 广告身份验证 Asp.net Core 2.2 和 Angular 8.2 客户端的 CORS 问题
问题描述
我正在使用工作或学校帐户身份验证设置新的 ASP.NET Core Web API。该 API 将由 Angular 8.2 应用程序使用。我从 Nuget 添加了 CORS(Microsoft.AspNetCore.Cors - 2.2.0 版)并将我的 Startup.cs 文件配置为 AddCors 和 UseCors。我已配置为 AllowAnyOrigin、AllowAnyMethod、AllowAnyHeader。我的 UseCors 语句也在 UseMVC 语句之上。
该 API 在 Web 浏览器上独立执行时可以正常工作。但是,当我将其插入 Angular 应用程序时,我遇到了错误。
从源“ http://localhost:4200 ”访问“ https://localhost:44342/api/Values ”处的 XMLHttpRequest已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头出现在请求的资源。
为什么 AllowAnyOrigin 没有被识别?
有人能帮我吗?
解决方案
下面的一段代码对我很有魅力:
公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
app.UseAuthentication();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader());
app.UseMvc();
}
我在这里找到的。
有几篇关于类似问题的帖子,这里是相同的链接,请检查并让我知道它是否对您有用。
Access-Control-Allow-Origin 标头如何工作?
在 azure 应用服务上运行的 Angular 应用未收到由 .net 核心 Web api 应用在 azure 上发送的 .AspNetCore.Antiforgery cookie
希望它有所帮助,请随时在您的任何对话中标记我。
推荐阅读
- sql-server - 测试 Active Directory 帐户的 SQL Server 连接
- linux - 如何设置 HAProxy 以在匹配的查询字符串键值上断开连接?
- spring - 类中的 MongoTemplate 空指针异常
- php - Ajax 请求但没有从 PHP 脚本中得到答案
- android - Firebase 列表适配器:错误“空对象引用”
- vba - 允许 VBA 时限制 Word 文档
- google-apis-explorer - 无法在我的某些链接上获取 google url 缩短器的分析
- python-3.x - 使用 Tensorflow 的简单逻辑回归:损失和权重根本没有变化
- command-prompt - 如何创建一个 ms-dos 7.10 程序?
- c# - MySql.Data.MySqlClient.MySqlException :'已经有一个打开的 DataReader 与此连接关联,必须先关闭。'