c# - 浏览器OPTIONS请求方式和windows授权

标签 c# iis asp.net-web-api windows-authentication angular

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

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

这解决了我授权 GET 请求的问题,现在对于第一个 GET 请求,服务器返回带有 header 的 401 响应:

WWW-Authenticate:Negotiate
WWW-Authenticate:NTLM

然后, Angular 客户端发送另一个请求,但这次使用包含 NTLM token 的授权 header 并且它有效。

对于 POST 请求,我在请求的 header 中添加了“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 身份验证? 或者也许有一些方法可以强制浏览器跟进授权?

最佳答案

好的,我找到了一种方法,可以让它在 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 授权。

关于c# - 浏览器OPTIONS请求方式和windows授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34766583/

相关文章:

c# - 行分隔的 json 序列化和反序列化

iis - 创建网站后运行的 Azure 启动任务

PHP 错误 - 上传文件

c# - 在 web api 中正确实现 "windows"身份验证?

c# - Web-API 版本控制不适用于默认版本

c# - PredicateBuilder Where List inside List with C#

c# - 使用初始值设定项创建对象数组似乎失败

c# - 对不同的控件使用与事件相同的功能是不是一个坏主意?

c# - WMI - 读取远程域计算机中的 IIS 应用程序时抛出异常 'Access Denied'

c# - 如何将 DLL 引用添加到 .NET WebAPI 项目?