来自 angular 的 Owin 令牌调用 4 请求的资源上不存在 'Access-Control-Allow-Origin' header

Owin token call from angular 4 No 'Access-Control-Allow-Origin' header is present on the requested resource

我正在使用 owin 在 asp.net webapi 中生成 oAuth 令牌,并且我在 Statrup.cs

的配置中将 UserCors 添加为第一行
public void Configuration(IAppBuilder app)
{
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    ConfigureOAuth(app);
}

我也在ConfigureOAuth(app)里面又加了一遍;

我手动尝试在 GrantResourceOwnerCredentials 中添加 header,如下所示

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

但是没有用。我一直收到错误 angular 端请求的资源上没有 'Access-Control-Allow-Origin' header。

当我在配置文件中添加 header 时,它说重复。

<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT,DELETE" />
</customHeaders>

不确定发生了什么。

技巧是从启动文件中删除 Global.asax 并调用 WebApiConfig.Register。 帮助了我。