System.Web.Http.Cors配置跨域訪問的兩種方式

來源:互聯網
上載者:User

標籤:透明   跨域   api   自訂   cti   var   nodejs   pps   let   

System.Web.Http.Cors配置跨域訪問的兩種方式

  使用System.Web.Http.Cors配置跨域訪問,眾多大神已經發布了很多文章,我就不在詳細描述了,作為小白我只說一下自己的使用心得。在webapi中使用System.Web.Http.Cors配置跨域資訊可以有兩種方式。 
  一種是在App_Start.WebApiConfig.cs的Register中配置如下代碼,這種方式將在所有的webapi Controller裡面起作用。

using System;using System.Collections.Generic;using System.Linq;using System.Web.Http;using System.Web.Http.Cors;namespace YDTG.Service{ public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API 配置和服務 // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); //這是重點,從設定檔的appsettings節點中讀取跨域的地址 var cors = new EnableCorsAttribute(ConfigurationManager.AppSettings["origins"], "*", "*"); config.EnableCors(cors); } }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

設定檔如下,注意一定要加上http

<add key="origins" value="http://localhost:9012,http://192.168.1.108:9012" />
  • 1
  • 1

  第二種方式就是在每個webapiController類中設定,即每個控制器個人化配置,如下代碼。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Http.Cors;using System.Web.Mvc;namespace Service.Controllers{ [EnableCors("http://localhost:9012,http://192.168.1.108:9012", "*", "*")] public class HomeController : Controller { public ActionResult Index() { ViewBag.Title = "Home Page"; return View(); } }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
注意事項
  1. EnableCors共三個參數分別為origins、headers和methods。origins配置允許訪問的網域名稱,多個網域名稱以逗號分隔即可,網域名稱一定要完整,如果是ip地址前面要加上“http”,只使用IP的話一定會失效的。參數headers配置所支援的資源。參數methods配置支援的方法,get、post、put等。如果允許任意網域名稱、任意資源、任意方法訪問自己的webapi,則三個參數全部使用星號”*”即可。
  2. “EnableCors(“http://localhost:9012,http://192.168.1.108:9012“, ““, ““)”中的配置如果出現錯誤的話不會報錯,而是直接禁止未出現在配置表中的資源訪問。
  3. 如果使用第一種方式那麼可以從設定檔中讀取網站列表,如果使用第二種方式,所有的參數只能使用常量。

 

 

 

http://blog.csdn.net/chaoyangzhixue/article/details/52251322

 

會得到CORS Access Deny,即跨域訪問被拒絕。

對於C#做如下配置可允許資源的跨域訪問: 

<system.webServer>
...
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type" />
<add name="Access-Control-Allow-Methods" value="PUT,GET,POST,DELETE,OPTIONS"/>
</customHeaders>
</httpProtocol>
</system.webServer>

對於nodejs做如下配置可允許資源的跨域訪問: 

//設定CORS跨域訪問
app.all(‘*‘, function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, accept, origin, content-type");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ‘ 3.2.1‘)
res.header("Content-Type", "application/json;charset=utf-8");
next();
});

Access-Control-Allow-Origin:*表示允許任何域發起請求,如果只允許特定的域訪問,則設定Access-Control-Allow-Origin:xxx為具體網域名稱即可。

 

Preflighted Requests(預檢請求)

Preflighted Requests是CORS中一種透明伺服器驗證機制。預檢請求首先需要向另外一個網域名稱的資源發送一個 HTTP OPTIONS 要求標頭,其目的就是為了判斷實際發送的請求是否是安全的。

下面的2種情況需要進行預檢:

1、  簡單請求,比如使用Content-Type 為 application/xml 或 text/xml 的 POST 請求;

2、中設定自訂頭,比如 X-JSON、X-MENGXIANHUI 等。

 

 

http://blog.csdn.net/chaoyangzhixue/article/details/52251322

 

System.Web.Http.Cors配置跨域訪問的兩種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.