標籤:style http color io os 使用 ar for art
微軟出了一套解決方式能夠解決 “同意WebAPI的 CORS 請求”
http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api
假設你同一時候使用了OWIN,即使使用了這個,也不能使OWIN 同意 cross-origin 請求。
OWIN 是基於kantana的一套OAUTH2.0解決方式。
所以要使用 DLL Microsoft.OWIN.Cros
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);
In WebApiConfig.cs, under public static void Register(HttpConfiguration config), I added in these lines:
// Corsvar cors = new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS");config.EnableCors(cors);
須要注意的是:
app.UseCors(CorsOptions.AllowAll) - enables CORS for allcross-origins requests to your site.
config.EnableCors(..) enables CORS for Web Api only
意思就是:
假設你使用了Microsoft.Owin.Cors,然後在Startup.Auth.cs中加上了app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
就不須要再用config.EnableCors(..)了。
或者 最直接的方法()!!!!
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
</customHeaders>
</httpProtocol>
把這段加在web.config的<system.webServer>節點下,成功!
Enable OWIN Cross-origin Request