如何修复“CORS 协议不允许同时指定通配符(任何)来源和凭据"?错误 [英] How to fix "The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time" error

查看:61
本文介绍了如何修复“CORS 协议不允许同时指定通配符(任何)来源和凭据"?错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've already enabled CORS on the project in C# .net Core

startup.cs 中我添加了行

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

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

But when I try to use API in another Blazor project I see in logs in my API project on Host this error

CORS 协议不允许指定通配符(任何)来源和凭据.通过列表配置策略如果需要支持凭据,则为单个来源

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 之前,我在浏览器控制台中看到另一个错误

Before I enable Cors I see another error in the browser console

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

What can I change for solving it?

推荐答案

您应该提供其余的代码...这是 Blazor 客户端应用程序还是 Razor 组件应用程序(以前称为服务器端 Blazor)?我猜这是一个 Blazor 客户端应用程序,对吧?为什么要实例化 HttpClient ?您应该改用 DI(也许是构造函数注入),注入 Blazor 本身提供的 HttpClient 实例.

You should have provided the rest of your code... Is this a Blazor client application or Razor Components application (formally known as Server-Side Blazor) ? I guess this is a Blazor client application, right ? Why do you instantiate an HttpClient ? You should use DI (Perhaps Constructor Injection) instead, injecting an HttpClient instance provided by Blazor itself.

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

The problem is probably server side, though it surfaces as a client one... Try the following:

获取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 中启用.请参阅 https://docs.microsoft.com/en-us/aspnet/core/security/cors 有关如何在 ASP.NET Core 中启用 CORS 的详细信息.

Note, once again: CORS needs to be enabled on the server side, not in blazor. See https://docs.microsoft.com/en-us/aspnet/core/security/cors for details on how to enable CORS in ASP.NET Core.

Blazor:

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


@functions {

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

    }

}  

希望这有帮助...

这篇关于如何修复“CORS 协议不允许同时指定通配符(任何)来源和凭据"?错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆