场景很简单,我需要从另一台服务器(与API服务器不同)登录来检索访问令牌。
我在API服务器上安装了Microsoft.Owin.Cors
包。在public void ConfigureAuth(IAppBuilder app)
下的Startup.Auth.cs
文件中,我添加了
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
在WebApiConfig.cs
中,在public static void Register(HttpConfiguration config)
下,我添加了以下几行:
// Cors
var cors = new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS");
config.EnableCors(cors);
我还应该改变什么?
发布于 2013-11-20 04:56:23
看看我找到了什么!
在<system.webServer>
中添加一些自定义标头。
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
</customHeaders>
</httpProtocol>
然后我就可以做CORS认证了。
发布于 2014-09-26 04:27:53
为了详细阐述Youngjae的答案,在http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/上有一个很好的教程,介绍了如何使用Web API设置OWIN并在该过程中启用CORS
您需要使用以下命令添加CORS的NuGet包:
Install-Package Microsoft.Owin.Cors -Version 2.1.0
然后添加
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
添加到Startup.cs中的配置方法中,使其看起来如下所示:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
发布于 2015-03-04 22:46:43
我的答案在
Web Api 2 Preflight CORS request for Bearer Token
具体地说,使用OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials实现的/Token请求再次添加了标头。在任何其他OWIN配置之前添加OWIN CORS内容,并根据该链接从GrantResourceOwnerCredentials中删除头文件,然后就是这样。祝好运。
https://stackoverflow.com/questions/20079813
复制相似问题