当前位置:首页 > 操作系统 > Windows

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

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

<code>var request = new XMLHttpRequest();
request.withCredentials = true;
</code>

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

<code>WWW-Authenticate:Negotiate
WWW-Authenticate:NTLM
</code>

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

对于POST请求,我在请求的标头中添加了“ Content-Type:application / json”,因此浏览器发送了第一个请求,如下所示:

<code>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
</code>

服务器响应:

<code>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
</code>

但是这一次,有一个错误,而不是像GET请求那样的另一个具有授权的请求:

<code>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.
</code>

对于CORS,我在ASP.NET 5中使用以下配置:

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

我可以以某种方式为IIS中的OPTIONS请求禁用Windows身份验证吗?
或者,也许有某种方法可以迫使浏览器跟进授权?

解决方法:

好的,我找到了一种使用ASP.NET 5在IIS Express或IIS 8.5上运行的方法.

我们需要像这样修改wwwroot / web.config:

<code><?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>
</code>

在launchSettings.json中设置:

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

在Startup.cs中:

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

其中一些设置可能不是必需的.

对于IIS,我们需要安装Windows身份验证和URL授权.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐