微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#-浏览器选项请求方法和Windows授权

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

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

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

WWW-Authenticate:Negotiate
WWW-Authenticate:NTLM

然后,该角度客户端发送了另一个请求,但是这次带有包含NTLM令牌的Authorization标头,并且它可以工作.

对于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身份验证吗?
或者,也许有某种方法可以迫使浏览器跟进授权?

解决方法:

好的,我找到了一种使用ASP.NET 5在IIS Express或IIS 8.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授权.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐