我正在使用工作或学校帐户身份验证设置新的ASP.NET Core Web API。 Angular 8.2应用程序将使用此API。我从Nuget添加了CORS(Microsoft.AspNetCore.Cors-版本2.2.0),并将Startup.cs文件配置为AddCors和UseCors。我已经配置为AllowAnyOrigin,AllowAnyMethod,AllowAnyHeader。我的UseCors语句也在UseMVC语句之上。
当在网络浏览器上独立执行时,API可以正常工作。但是,当我将其插入Angular App时,出现了以下错误。
源C https://localhost:44342/api/Values从源http://localhost:4200对XMLHttpRequest的访问已被CORS策略阻止:所请求的资源上没有'Access-Control-Allow-Origin'标头。
为什么无法识别AllowAnyOrigin?
有人可以帮我吗?
下面的代码对我来说很有吸引力:
public void Configure(IApplicationBuilder应用程序,IHostingEnvironment env){如果(env.IsDevelopment()){app.UseDeveloperExceptionPage();}
app.UseAuthentication();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader());
app.UseMvc();
}
我发现here。
关于类似问题,有几篇文章,这是相同的链接,请检查并让我知道它是否对您有用。
How does Access-Control-Allow-Origin header work?
ASP.NET5, MVC 6 Cookies not being shared across sites
希望它会有所帮助,随时在您的任何谈话中为我加标签。