我有一个asp.net.core 2.2 API,我一直在用招摇过头进行测试,到目前为止一切工作正常。一旦我进入角度并尝试发布一些数据,我就一直被cors拒绝。这是我在cors中的配置。
services.AddCors(options => options.AddPolicy("AllowAllOrigins",
builder =>
builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()));
和配置:
app.UseCors("AllowAllOrigins");
app.UseMvc();
现在的问题是我遇到了这个“错误”:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request
starting HTTP/2.0 OPTIONS https://localhost:44337/api/Auth/Register
Microsoft.AspNetCore.Cors.Infrastructure.CorsService:Information: CORS
policy execution successful.
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request
finished in 7.566ms 204
这是我发布的角度代码:
Register(body: string) {
console.log(body);
const header = new HttpHeaders()
.set('Content-type', 'application/json');
return this.http.post('https://localhost:44337/api/' + 'Auth/Register'
, body, {observe: 'response', headers: header, withCredentials: true});
}
这是动作声明:
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Register([FromBody] RegisterModel model)
一切正常2.1,但现在我无法收到对API的任何请求。如果我删除了allowcredentialsentials,cors将指定我无权访问,依此类推。但是现在他似乎什么也没做。没错,没事。