Asp.Net Cors在OPTIONS上返回标题,但不在GET请求上返回

问题描述 投票:1回答:1

在.Net Mvc(非核心)项目中使用Microsoft.Owin.Cors我在启动时配置cors如下:

var cors = new CorsPolicy {
  AllowAnyHeader = true,
  SupportsCredentials = true,
};
cors.Origins.Add("http://localhost:3006");
cors.Methods.Add("GET");
cors.Methods.Add("POST");
cors.Methods.Add("PUT");
cors.Methods.Add("DELETE");
cors.Methods.Add("OPTIONS");
app.UseCors(new CorsOptions {
  PolicyProvider = new CorsPolicyProvider {
    PolicyResolver = ctx => Task.FromResult(cors)
  }
});

当我从localhost:3006上的站点执行跨源查询时,我获得了成功的OPTIONS预检请求,然后是成功但已阻止的GET请求。

OPTIONS followed by a blocked GET

当我检查响应时,我明白了

access-control-allow-credentials: true
access-control-allow-headers: authorization
access-control-allow-origin: http://localhost:3006

在OPTIONS响应中,但不在GET上!

为什么不呢?我需要什么才能使其正常工作?

asp.net http cors owin
1个回答
0
投票

因此,在插入这一整天后,我终于开始工作了。我无法使用Owin处理程序工作。对我的暗示是OPTIONS成功但GET失败了。

我怀疑在.Net Framework上,Mvc实际上发生在Owin管道之外,因为它直接绑定到System.Web。虽然我确信通过搞乱我的web.config可以解决这个问题。在我的情况下,我真的只需要CORS来处理我的web api处理路由,因此切换到Microsoft.AspNet.WebApi.Cors就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.