gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:27:53 27 4
gpt4 key购买 nike

<分区>

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

Response for preflight has invalid HTTP status code 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 header ,如下所示

        <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 身份验证 block 中也有以下内容

             <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 Controller 代码

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

}
}

来 self 的 Angular 代码,但它没有帮助。我也尝试用“text/html”替换它,但这也不起作用。有人能告诉我我做错了什么吗?正如我所假设的那样,如果它适用于 GET,它应该可以正常工作。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com