gpt4 book ai didi

c# - Angular2 中的 Http PUT 到 .NET Core Web API 从预检请求中给出了 http 401 错误

转载 作者:行者123 更新时间:2023-11-30 23:20:20 25 4
gpt4 key购买 nike

我有一个 Angular2 应用程序,它对 .NET Core Web API Controller 执行 http PUT。当我运行应用程序时,它首先发出 OPTIONS 预检请求并抛出 401 Unauthorized 错误。

XMLHttpRequest cannot load http://localhost:55137/api/Foo/2. Response for preflight has invalid HTTP status code 401

我试过直接从 Postman 执行 PUT,它工作正常。不知道这是否相关,但我来自 Angular 应用程序的 GET 也工作正常。在文档和网络搜索的帮助下,我已经将以下代码放在一起,但我还没有完全理解为什么 OPTIONS 飞行前请求未经授权?我尝试了一些操作,例如在 web.config 中删除和重新添加 OPTIONSVerbHandler。我还读到预检请求要求您不要发送凭据,但我不确定在我的情况下该怎么做。

我正在使用 Angular 2.0.0-rc.5 和 .NET Core 1.0.0,并在 IIS 8.5 上托管(在 Visual Studio 2015 中开发时使用 IISExpress)。

foo.service.ts:

import { Injectable } from "@angular/core";
import { Headers, Http, RequestOptions } from "@angular/http";
import { CONFIG } from "./../app.config";

@Injectable()
export class FooService {
private _apiUrl: string = CONFIG.generalAPI;
private _headers: Headers = new Headers({ "Content-Type": "application/json" });

constructor(private http: Http) { }

updateObj(fooObj: any): Promise<any> {
let body: any = JSON.stringify(fooObj);
let options: RequestOptions = new RequestOptions({
headers: this._headers,
withCredentials: true
});
let result: Promise<any> = this.http.put(this._apiUrl + "/Foo/" + fooObj.ID, body, options)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
return result;
}

private handleError(error: any): Promise<any> {
console.error("An error occurred", error);
return Promise.reject(error.message || error);
}
}

fooController.cs:

// PUT api/Foo/5
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody]FooClass obj)
{
try
{
_fooService.UpdateFoo(obj);
return Ok();
}
catch (Exception ex)
{
return BadRequest("An error occurred. Message: " + ex.Message);
}
}

webAPI 应用的 web.config:

<customHeaders>
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Origin" value="http://localhost:44612" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization" />
</customHeaders>

最佳答案

除了 web.config,你能在你的 ASP.NET 核心 web api 中像这样启用 CORS-

首先,在project.json中添加依赖——"Microsoft.AspNetCore.Cors": "1.0.0",

然后像这样在 startup.cs 中启用 CORS-

app.UseCors(builder => {
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});

如果您想限制特定来源,那么您可以这样做-

app.UseCors(builder => builder.WithOrigins("example.com"));

您可以找到有关 CORS 的更多信息 here

关于c# - Angular2 中的 Http PUT 到 .NET Core Web API 从预检请求中给出了 http 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801670/

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