ajax 如何禁止 预检请求,ajax - 预检请求正常,然后,在auth之后,响应不包含allow cors标头 - 堆栈内存溢出...

在IIS 8.5上部署的Asp MVC 5应用程序。 需要从许多客户端启用ajax请求。

服务器端我有WebApiConfig.cs

config.EnableCors();

在控制器中:

[EnableCors(origins: "http://localhost:59901", headers: "*", methods: "*", SupportsCredentials = true)]

public class ItemController : Controller

客户端

$("#getItem").on("click", function (e) {

var myurl = "http://servername/item/details/1"

$.ajax({

url: myurl,

type: "GET",

dataType: "JSON",

xhrFields: {

withCredentials: true

},

contentType: "application/json; charset=utf-8",

error: function (jqXHR, textStatus, errorThrown) {

$('#result').text(jqXHR.responseText || textStatus);

},

success: function (result) {

$('#result').text(result);

}

});

});

从VisualStudio Origin运行客户端是http:// localhost:59901 。

运行ajax请求我在fiddler中得到以下内容:

1.飞行前请求/响应

OPTIONS http://vrtsrv01.webdev.local/item/details/1 HTTP/1.1

Host: vrtsrv01.webdev.local

Connection: keep-alive

Access-Control-Request-Method: GET

Origin: http://localhost:59901

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36

Access-Control-Request-Headers: content-type

Accept: */*

Referer: http://localhost:59901/Home/Index

Accept-Encoding: gzip, deflate, sdch

Accept-Language: en-US,en;q=0.8,it;q=0.6,it-IT;q=0.4

HTTP/1.1 200 OK

Server: Microsoft-IIS/8.5

Access-Control-Allow-Origin: http://localhost:59901

Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, MaxDataServiceVersion

Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS

Access-Control-Allow-Credentials: true

X-Powered-By: ASP.NET

Date: Sat, 13 May 2017 15:34:54 GMT

Content-Length: 0

2. GET请求没有凭据/ 401错误响应

GET http://vrtsrv01.webdev.local/item/details/1 HTTP/1.1

Host: vrtsrv01.webdev.local

Connection: keep-alive

Accept: application/json, text/javascript, */*; q=0.01

Origin: http://localhost:59901

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36

Content-Type: application/json; charset=utf-8

Referer: http://localhost:59901/Home/Index

Accept-Encoding: gzip, deflate, sdch

Accept-Language: en-US,en;q=0.8,it;q=0.6,it-IT;q=0.4

HTTP/1.1 401 Unauthorized

Cache-Control: private

Content-Type: text/html

Server: Microsoft-IIS/8.5

X-AspNet-Version: 4.0.30319

WWW-Authenticate: Negotiate

WWW-Authenticate: NTLM

X-Powered-By: ASP.NET

Date: Sat, 13 May 2017 15:34:54 GMT

Content-Length: 1352

Proxy-Support: Session-Based-Authentication

401 - Autorizzazione negata: accesso negato a causa di credenziali non valide.

....

Errore del server

....

3.使用NTLM令牌对GET请求进行身份验证/响应,而不允许使用CORS标头

GET http://vrtsrv01.webdev.local/item/details/1 HTTP/1.1

Host: vrtsrv01.webdev.local

Connection: keep-alive

Authorization: Negotiate <...ntlm token here ...>

Accept: application/json, text/javascript, */*; q=0.01

Origin: http://localhost:59901

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36

Content-Type: application/json; charset=utf-8

Referer: http://localhost:59901/Home/Index

Accept-Encoding: gzip, deflate, sdch

Accept-Language: en-US,en;q=0.8,it;q=0.6,it-IT;q=0.4

HTTP/1.1 200 OK

Cache-Control: private

Content-Type: application/json; charset=utf-8

Server: Microsoft-IIS/8.5

X-AspNetMvc-Version: 5.2

X-AspNet-Version: 4.0.30319

Persistent-Auth: true

X-Powered-By: ASP.NET

Date: Sat, 13 May 2017 15:34:58 GMT

Content-Length: 8557

{"id":1, .....}

为什么在为CORS启用MVC应用程序并查看对预检请求的正确响应后,NTLM身份验证后获得的响应不包含预期的Access-Control-Allow-Origin标头?


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部