浏览器的OPTIONS请求方法和Windows授权

3

我正在尝试从我的Angular 2服务向使用Windows身份验证并托管在IIS上的ASP.NET 5 API POST数据。 在对Angular进行了一些修改后,请求被创建为:

var request = new XMLHttpRequest();
request.withCredentials = true;

这解决了我在授权GET请求方面的问题,现在对于第一个GET请求,服务器返回带有以下标头的401响应:
WWW-Authenticate:Negotiate
WWW-Authenticate:NTLM

接着,Angular客户端发送另一个请求,但这次带有包含NTLM令牌的授权头,它可以正常工作。

对于POST请求,我将"Content-Type: application/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的实现,并在Angular DI中进行了注册。 - Adiqq
1个回答

4

好的,我找到了一种方法,可以使其在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
没错!我也遇到了同样的问题,但我的web.config文件不需要更改。我只需启用匿名身份验证,POST请求也能正常工作。谢谢! - David
@pajics 我知道现在问有点晚了.. 但是在 Web 表单的情况下应该怎么做(不是 MVC)? - Sandeep Thomas
@SandeepThomas,很抱歉我无法帮助你解决这个问题,但也许这个SO链接可以帮到你: https://dev59.com/glsV5IYBdhLWcg3w6ych - pajics

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接