如何修复 "The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time" 错误

How to fix "The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time" error

我已经在 C# .net Core 项目中启用了 CORS

startup.cs中我添加了行

...
services.AddCors();
...
app.UseCors(builder => builder
    .AllowAnyOrigin()
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials());

但是当我尝试在另一个 Blazor 项目中使用 API 时,我在主机上的 API 项目的日志中看到这个错误

The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the policy by listing individual origins if credentials needs to be supported

我的代码在Blazor

using (HttpClient http = new HttpClient()) {
  http.DefaultRequestHeaders.Add("Authorization", "Token");
   var response = await http.GetStringAsync("https://example.com?prm=2");
   Console.WriteLine(response);
   dynamicContent = response;
}

在启用 Cors 之前,我在浏览器控制台中看到另一个错误

我可以改变什么来解决它?

您应该已经提供了其余的代码... 这是 Blazor 客户端应用程序还是 Razor Components 应用程序(正式名称为服务器端 Blazor)? 我想这是一个 Blazor 客户端应用程序,对吧? 为什么要实例化 HttpClient ?您应该改用 DI(可能是构造函数注入),注入 Blazor 本身提供的 HttpClient 实例。

问题可能出在服务器端,尽管它表现为客户端... 尝试以下操作:

获取https://www.nuget.org/packages/Microsoft.AspNetCore.Cors/

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader());
    });
     .....
}

还有这个:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)    
{
      app.UseCors("CorsPolicy");
}

请再次注意:需要在 服务器端 启用 CORS,而不是在 blazor 中启用。有关如何在 ASP.NET Core 中启用 CORS 的详细信息,请参阅 https://docs.microsoft.com/en-us/aspnet/core/security/cors

Blazor:

 @page "/<template>"
 @inject HttpClient Http


@functions {

    protected override async Task OnInitAsync()
    {
        var response= await Http.GetJsonAsync<string>    
                      ("https://example.com?prm=2");

    }

}  

希望这对您有所帮助...

我遇到了同样的问题,我删除了 AllowCredentials() 解决了我的问题。

有点晚了,但我希望它能对某人有所帮助。

如果您想 AllowCredentials()AllowAnyOrigin() 一起使用,只需使用 SetIsOriginAllowed(Func<string,bool> predicate)

doc about IsOriginAllowed

        services
            .AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    );

                options.AddPolicy("signalr",
                    builder => builder
                    .AllowAnyMethod()
                    .AllowAnyHeader()

                    .AllowCredentials()
                    .SetIsOriginAllowed(hostName => true));
            });

我也遇到了同样的问题,我在这里找到了解决方案:

Setup Any Origin And Any Credentials

像这样在 startup.cs 文件中更改您的 CORS 设置

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddCors(options =>
    {
        options.AddDefaultPolicy(builder => 
            builder.SetIsOriginAllowed(_ => true)
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
    });
}

对我有用。

您不能同时使用 AllowAnyOrigin()AllowCredentials() 所以将您的代码更改为:

...
services.AddCors();
...
app.UseCors(builder => builder
    .WithOrigins("https://example.com")
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials());

我有同样的问题,通过从 URL 的结尾删除 slash( / ) 解决了这个问题,因为我总是从 chrome 浏览器复制和粘贴 url,它的末尾有 / :

  .WithOrigins("https://localhost:60576/")  // not working 

但是

  .WithOrigins("https://localhost:60576")  // working  !!!

    

第一步 安装 nuGet 包:
Microsoft.AspNetCore.Cors

步骤 2 添加

services.AddCors();

在 startup.cs 下的 ConfigureServices

步骤 3 添加

    app.UseCors(x => x
                .AllowAnyMethod()
                .AllowAnyHeader()
                .SetIsOriginAllowed(origin => true) // allow any origin
                .AllowCredentials());

在 startup.cs 配置下