发布后被IIS上的CORS策略阻止- ASP.NET Core Web API不工作

nlejzf6q  于 2023-10-19  发布在  .NET
关注(0)|答案(2)|浏览(243)

我的项目在Visual Studio上运行得很好,但是在我发布了ASP.NET Core Web API并构建了Angular代码之后,由于这个错误,我无法访问API函数:
CORS策略已阻止从源“http://localhost:44413”访问位于“http://localhost:7173/login/login”的XMLHttpRequest:对印前检查请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

我检查了网络选项卡,发现没有向请求中添加http标头。
我真的不知道为什么出版后会发生这种情况。

  1. var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
  2. builder.Services.AddCors(options =>
  3. {
  4. options.AddPolicy(name: MyAllowSpecificOrigins,
  5. builder =>
  6. {
  7. builder.AllowAnyOrigin()
  8. .AllowAnyHeader()
  9. .AllowAnyMethod()
  10. .AllowCredentials();
  11. });
  12. });
  13. ......
  14. app.UseCors(MyAllowSpecificOrigins);

这是角调用:

  1. let httpOptions = {
  2. headers: new HttpHeaders({
  3. 'Access-Control-Allow-Origin': '*',
  4. 'Access-Control-Allow-Methods': '*'
  5. })
  6. };
  7. this.http.post<string>('http://localhost:7173/login/login', this.user, httpOptions)
  8. .subscribe(results => { console.log(results); },
  9. error => console.log(error));

这是我的代理JSON:

  1. {
  2. "/*": {
  3. "target": "http://localhost:7173/",
  4. "secure": false,
  5. "logLevel": "debug"
  6. }
  7. }

我做了一切,改变了一切,但错误仍然是一样的。
在visual studio上一切都运行得很好,但是在发布之后,我不知道是什么原因。

plupiseo

plupiseo1#

  1. builder.Services.AddCors(options => options.AddPolicy(name: "AppOrigins",
  2. policy => { policy.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader(); }));
  3. app.UseCors("AppOrigins");

https://github.com/mammadkoma/WebApi/blob/master/WebApi/Program.cs
服务和中间件的顺序很重要。

xwbd5t1u

xwbd5t1u2#

我终于在这个视频中通过手册做到了这一点:
https://www.youtube.com/watch?v=Lt3wve_nb0g
这与Cors或其他东西无关,问题是后端ASP.NET Core Web API没有运行。在IIS中,在池中,我们应该在相关池中设置32位应用程序激活为true。
对于发布,我们必须以管理员身份运行Visual Studio。

相关问题