如何使CORS在验证2的WebAPI? [英] How to make CORS Authentication in WebAPI 2?
问题描述
该场景很简单,我需要从另一台服务器(从API服务器不同)登录检索访问令牌。
The scenario is simple, I need to log in from another server (different from the API server) to retrieve the access token.
我装 Microsoft.Owin.Cors
封装API服务器上。在 Startup.Auth.cs
文件,在公共无效ConfigureAuth(IAppBuilder应用)
,我
I installed Microsoft.Owin.Cors
package on the API Server. In Startup.Auth.cs
file, under public void ConfigureAuth(IAppBuilder app)
, I added in
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
在 WebApiConfig.cs
,在公共静态无效的注册(HttpConfiguration配置)
,我在这行补充说:
In WebApiConfig.cs
, under public static void Register(HttpConfiguration config)
, I added in these lines:
// Cors
var cors = new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS");
config.EnableCors(cors);
还有什么我应该改变?
What else should I change?
推荐答案
看看我发现!
添加一些自定义标题里面的< system.webServer方式>
Add in some custom headers inside <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认证。
Then I can do the CORS authentication.
这篇关于如何使CORS在验证2的WebAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!