2016-01-13 17 views
3

我想從我的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身份驗證? 或者,也許有某種方法來強制瀏覽器跟進授權?

+0

你能提供更多關於如何獲得GET請求的信息嗎?我正在使用angular的http.get標頭設置withCredentials = true,但似乎永遠不會發送授權標頭/標記。 – alrm3000

+0

這是相當不好的,但我取代了[BrowserXhr]的實現(https://github.com/angular/angular/blob/master/modules/%40angular/http/src/backends/browser_xhr.ts),並將其註冊到角度DI。 – Adiqq

回答

3

好吧,我找到了一種方法,使其在IIS Express或IIS 8.5的工作,與ASP.NET 5.

我們需要修改的wwwroot/web.config中像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer> 
    <handlers> 
     <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" /> 
    </handlers> 
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600" forwardWindowsAuthToken="true"></httpPlatform> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Request-Headers" value="Content-Type,Authorization" /> 
     <add name="Access-Control-Allow-Headers" value="Content-Type,Authorization" /> 
     <add name="Access-Control-Allow-Origin" value="http://localhost:5814" /> 
     <add name="Access-Control-Allow-Credentials" value="true" /> 
     </customHeaders> 
    </httpProtocol> 
    <security> 
     <authorization> 
     <!--<remove users="*" roles="" verbs="" /> Uncomment for IIS--> 
     <add accessType="Allow" users="*" verbs="GET,POST,PUT" /> 
     <add accessType="Allow" users="?" verbs="OPTIONS" /> 
     <add accessType="Deny" users="?" verbs="GET,POST,PUT" /> 
     </authorization> 
    </security> 
    </system.webServer> 
    <system.web> 
    <authorization> 
     <allow users="*" verbs="GET,POST,PUT" /> 
     <allow users="?" verbs="OPTIONS" /> 
    </authorization> 
    </system.web> 
</configuration> 

在launchSettings.json設置:

"iisSettings": { 
"windowsAuthentication": true, 
"anonymousAuthentication": true, 
"iisExpress": { 
    "applicationUrl": "http://localhost:4402/", 
    "sslPort": 0 
} 

而且在Startup.cs:

services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyMethod().WithHeaders("accept", "authorization", "content-type", "origin", "x-custom-header").AllowCredentials())); 

其中一些設置可能不是必需的。

對於IIS,我們需要安裝Windows身份驗證和URL授權。

+2

必須允許啓用cors的匿名身份驗證(當然,app.UseMvc()必須位於app.UseCors()之後),因此它不會嘗試驗證cors預檢檢查。 web.config不需要修改。 – pajics

+0

它是真的!我有同樣的問題,我的web.config不必改變。我只啓用匿名身份驗證和POST請求也工作。謝謝! – David