Angular 4使用Windows身份验证进行HTTP POST时的预检问题

5

我正在使用带有启用了Windows身份验证的ASP.NET Web API 2的Angular 4。我有一个POST和一个GET API。GET正常工作。但是对于POST,我得到了以下错误:

预检请求的响应具有无效的HTTP状态代码401

这意味着我在OPTIONS方法中收到未经授权的错误,但我知道这不应该是情况,因为OPTIONS方法从不使用用户凭据调用。

在Angular中,我如下调用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标头设置在我的Web配置文件中,如下所示:

        <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();
        }

    }
}

我尝试从我的Angular代码中使用它,但没有帮助。我还尝试将其替换为"text/html",但也不起作用。有人能告诉我我做错了什么吗?因为我认为如果GET能够正常工作,那么POST应该也能正常工作。


你使用的是哪个 Web API 系统? - Bailey Miller
对于C# Web Api 2.0,您需要使用[HttpPost]属性声明一个post方法。 - Bailey Miller
@BaileyMiller 是的,我已经做了,并且添加了 actionname 和 route。 - nikhil mehta
我能看到更多实际的控制器代码吗? - Bailey Miller
@Paul,我的控制器没有 [Authorize]。我猜预检请求被触发了,因为内容类型头不是简单的,因为它的值是 application/json。 - nikhil mehta
显示剩余4条评论
2个回答

3
我读过的所有主题都指出,否则无法使用OPTIONS pre-flight请求,您不能完全禁用匿名身份验证。这是因为:
根据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>

这并没有帮助。 - nikhil mehta
1
我发现这个修复程序很好用。使用 IIS,我必须启用匿名和 Windows 验证。然后,在 web.config 中添加身份验证/授权设置。之后,由 Angular 发送到我的 API 网站的任何 OPTIONS 请求都能成功通过。 - Stuart

2

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

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


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