我在 Chrome 中收到以下错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIISPlatformHandler();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseCors(policy => policy
.WithOrigins("http://localhost:9000")
.AllowAnyMethod()
.WithHeaders("Access-Control-Allow-Origin, Content-Type, x-xsrf-token, Authorization")
.AllowCredentials());
app.UseMvc();
}
根据chrome,响应中没有添加单个 header 。
添加
access-control-allow-origin
的正确方法是什么? Asp.NET 5 中选项响应的 header ?
最佳答案
考虑一下 谷歌浏览器有 an issue它不支持本地主机通过 Access-Control-Allow-Origin。
在您的代码中,您已使用 AddCors
启用 CORS和 UseCors
Configure
中的方法方法,请确保您已按照 Specifying a CORS Policy 中提供的说明进行操作。 (用于 ConfigureServices
方法)和 How to enable CORS in ASP.NET 5
您也可以简单地写一个 Action Filter for plain Asp.net MVC controller .
CORS 的类型
Microsoft.AspNet.WebApi.Cors
: 使用它来启用 CORS 请求仅适用于 Web API。
Microsoft.AspNet.Cors
:使用它为 MVC Controller 启用 CORS。 Microsoft.Owin.Cors
: 使用它为所有跨域启用 CORS请求来到您的网站,例如,当您想要
为 Web API 和 SignalR 启用 CORS。
关于asp.net - 向 Asp.NET 中的选项预检请求添加 'access-control-allow-origin' 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34076463/