带有Windows身份验证预检问题的Angular 4 HTTP POST

时间:2017-08-03 21:37:49

标签: angular http cors windows-authentication preflight

我在后端使用带有ASP.NET web api 2的angular 4,并启用了Windows身份验证。我有一个帖子,一个得到api。 GET工作正常。但对于POST我收到此错误

  

预检的响应包含无效的HTTP状态代码401

这意味着我正在为OPTIONS方法获取未经授权的错误,我知道不应该这样,因为OPTIONS方法永远不会被用户凭证调用。

在角度我叫api如下

      let requestOptions = new RequestOptions({
      withCredentials: true,
      headers: new Headers({
        'content-type': 'application/json'
      })
    })
    this.http.post("someapiendpoint", JSON.stringify(data), requestOptions).subscribe(data => {
      debugger;
    });

我已在我的网络配置中设置了所有CORS标头,如下所示

        <add name="Access-Control-Expose-Headers " value="WWW-Authenticate"/>
        <add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
        <add name="Access-Control-Allow-Headers" value="accept, authorization, Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        <add name="Access-Control-Allow-Credentials" value="true" />

此外,我在web.config

的身份验证块中有以下内容
             <authentication>

                <anonymousAuthentication enabled="false" userName="" />

                <basicAuthentication enabled="false" />

                <clientCertificateMappingAuthentication enabled="false" />

                <digestAuthentication enabled="false" />

                <iisClientCertificateMappingAuthentication enabled="false">
                </iisClientCertificateMappingAuthentication>

                <windowsAuthentication enabled="true">
                    <providers>
                        <add value="Negotiate" />
                        <add value="NTLM" />
                    </providers>
                </windowsAuthentication>

            </authentication>

我尝试删除

headers: new Headers({
            'content-type': 'application/json'
          })

以下是我的web api控制器代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace Backend.Controllers
{
    public class DemoController : ApiController
    {
        Entities db = new Entities();
        public DemoController ()
        {
        }

        [HttpPost]
        [ActionName("postevent")]
        public IHttpActionResult PostEvent(Demo data)
        {
            db.Demos.Add(data);
            db.SaveChanges();
            return Ok();
        }

    }
}
从我的角度代码

但它没有帮助。我也尝试用&text; html&#39;替换它。但那也行不通。有人可以告诉我,我做错了什么。我认为如果它适用于GET,它应该可以正常工作。

2 个答案:

答案 0 :(得分:3)

我读过的所有主题都说明您无法完全禁用匿名身份验证,否则OPTIONS的航班前请求将无效。这是因为:

  

根据W3C规范,浏览器会从中排除用户凭据   CORS预检:

请参阅此主题,例如:WebAPI CORS with Windows Authentication - allow Anonymous OPTIONS request

基本上,解决方案是允许OPTIONS调用无需身份验证即可通过。为此,需要启用匿名身份验证。

在这个问题中:401 response for CORS request in IIS with Windows Auth enabled 答案建议使用以下web.config

<system.web>
  <authentication mode="Windows" />
    <authorization>
      <allow verbs="OPTIONS" users="*"/>
      <deny users="?" />
  </authorization>
</system.web>

答案 1 :(得分:1)

保罗的回答是正确的。我有完全相同的设置,它工作正常。

您需要确保同时启用匿名和Windows身份验证,从Paul对web.config的回答中添加片段以允许未经身份验证的OPTIONS请求,并在控制器上添加[Authorize]属性。

相关问题