CORS problem after migrating ASP web api app from .netcore2.* to net6.0

huangapple go评论112阅读模式
英文:

CORS problem after migrating ASP web api app from .netcore2.* to net6.0

问题

我们的代码在从netcore2.*升级到netcore3.0以及一直升级到net6.0的过程中遇到了问题。

根据MS文章的指南,我现在在我们的客户端应用程序中看到了这个错误:

从<url1>到<url2>的访问已被CORS策略阻止:请求的资源上没有'Access-Control-Allow-Origin'标头。如果不透明的响应满足您的需求,请将请求的模式设置为'no-cors',以禁用CORS获取资源。

是否有一种方法可以在进行身份验证的情况下实现通配符CORS?

新代码(net 6.0)

ConfigureServices 中:

  1. services.AddCors();
  2. services.AddMvc(o => o.EnableEndpointRouting = false)
  3. .AddNewtonsoftJson(options => { ... });

Configure 中:

  1. app.UseCors(configurePolicy =>
  2. {
  3. configurePolicy.AllowAnyOrigin();
  4. configurePolicy.AllowAnyHeader();
  5. configurePolicy.AllowAnyMethod();
  6. //configurePolicy.AllowCredentials();
  7. });
  8. app.UseMiddleware<OptionsMiddleware>();
  9. app.UseMiddleware<BearerTokenMiddleware>();
  10. app.UseWebSockets();
  11. app.UseMvc();

旧代码(net 2.2)- 工作正常!

ConfigureServices 中:

  1. services.AddCors();
  2. services.AddMvc()
  3. .AddJsonOptions(options => { ... });

Configure 中:

  1. app.UseCors(configurePolicy =>
  2. {
  3. configurePolicy.AllowAnyOrigin();
  4. configurePolicy.AllowAnyHeader();
  5. configurePolicy.AllowAnyMethod();
  6. configurePolicy.AllowCredentials();
  7. });
  8. app.UseMiddleware<OptionsMiddleware>();
  9. app.UseMiddleware<BearerTokenMiddleware>();
  10. app.UseWebSockets();
  11. app.UseMvc();

请注意上述 AddMvc 的更改以及在 UseCors 中注释掉 AllowCredentials 的需要。

项目类型为 Microsoft.NET.Sdk.Web

英文:

Our code is falling foul of changes made during the upgrade from netcore2.* to netcore3.0 during our upgrade all the way to net6.0.

Following the guidlelines from the MS Article mean I now see this error in our client application:

Access to fetch at &lt;url1&gt; from origin &lt;url2&gt; has been blocked by CORS policy: No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource. If an opaque response serves your needs, set the request&#39;s mode to &#39;no-cors&#39; to fetch the resource with CORS disabled.

Is there a way to achieve wildcard CORS with authentication?

NEW code (net 6.0)

In ConfigureServices:

  1. services.AddCors();
  2. services.AddMvc(o =&gt; o.EnableEndpointRouting = false)
  3. .AddNewtonsoftJson(options =&gt; { ... });

In Configure:

  1. app.UseCors(configurePolicy =&gt;
  2. {
  3. configurePolicy.AllowAnyOrigin();
  4. configurePolicy.AllowAnyHeader();
  5. configurePolicy.AllowAnyMethod();
  6. //configurePolicy.AllowCredentials();
  7. });
  8. app.UseMiddleware&lt;OptionsMiddleware&gt;();
  9. app.UseMiddleware&lt;BearerTokenMiddleware&gt;();
  10. app.UseWebSockets();
  11. app.UseMvc();

OLD code (net 2.2) - working!

In ConfigureServices:

  1. services.AddCors();
  2. services.AddMvc()
  3. .AddJsonOptions(options =&gt; { ... });

In Configure:

  1. app.UseCors(configurePolicy =&gt;
  2. {
  3. configurePolicy.AllowAnyOrigin();
  4. configurePolicy.AllowAnyHeader();
  5. configurePolicy.AllowAnyMethod();
  6. configurePolicy.AllowCredentials();
  7. });
  8. app.UseMiddleware&lt;OptionsMiddleware&gt;();
  9. app.UseMiddleware&lt;BearerTokenMiddleware&gt;();
  10. app.UseWebSockets();
  11. app.UseMvc();

Note the AddMvc changes above and the need to comment out AllowCredentials in UseCors.

Project type is Microsoft.NET.Sdk.Web.

答案1

得分: 1

我使用的代码是

  1. app.UseRouting();
  2. app.UseCors(x => x
  3. .AllowAnyMethod()
  4. .AllowAnyHeader()
  5. .SetIsOriginAllowed(origin => true) // 允许任何来源
  6. .AllowCredentials()); // 允许凭据

应该像这样工作 CORS problem after migrating ASP web api app from .netcore2.* to net6.0

英文:

The code I use is

  1. app.UseRouting();
  2. app.UseCors(x =&gt; x
  3. .AllowAnyMethod()
  4. .AllowAnyHeader()
  5. .SetIsOriginAllowed(origin =&gt; true) // allow any origin
  6. .AllowCredentials()); // allow credentials

It should work like this CORS problem after migrating ASP web api app from .netcore2.* to net6.0

huangapple
  • 本文由 发表于 2023年1月9日 19:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定