Enable OWIN Cross-origin Request

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.