我想從我的Angular 2服務發佈數據到使用Windows身份驗證並託管在IIS上的ASP.NET 5 API。瀏覽器選項請求方法和Windows授權
var request = new XMLHttpRequest();
request.withCredentials = true;
這解決了我的問題,授權GET請求,現在第一個GET請求,服務器返回401響應與頭:
WWW-Authenticate:Negotiate
WWW-Authenticate:NTLM
而且 一些修改角後,請求與創建之後角客戶端發送另一個請求,但這次使用包含NTLM令牌的授權標頭,並且它可以工作。
對於POST請求我加 「內容類型:應用程序/ JSON」,以請求的頭,所以瀏覽器發送這樣的第一個請求:
OPTIONS /api/reservation/ HTTP/1.1
Host: localhost:82
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:81
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:81/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
而且服務器響應:
HTTP/1.1 401 Unauthorized
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/8.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Wed, 13 Jan 2016 11:54:56 GMT
Content-Length: 6394
但這一次,而不是另一個有授權的請求,如GET請求中,有一個錯誤:
XMLHttpRequest cannot load http://localhost:82/api/reservation/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access. The response had HTTP status code 401.
對於CORS我用這個配置ASP.NET 5:
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin().AllowAnyMethod().WithHeaders("accept", "authorization", "content-type", "origin", "x-custom-header").AllowCredentials()));
我可以採用某種禁用IIS中OPTIONS請求Windows身份驗證? 或者,也許有某種方法來強制瀏覽器跟進授權?
你能提供更多關於如何獲得GET請求的信息嗎?我正在使用angular的http.get標頭設置withCredentials = true,但似乎永遠不會發送授權標頭/標記。 – alrm3000
這是相當不好的,但我取代了[BrowserXhr]的實現(https://github.com/angular/angular/blob/master/modules/%40angular/http/src/backends/browser_xhr.ts),並將其註冊到角度DI。 – Adiqq