草庐IT

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

coder 2024-06-02 原文

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

startup.cs 中我添加了行

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

但是当我尝试在另一个 Blazor 项目中使用 API 时,我在 Host 上的 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 之前,我在浏览器控制台中看到另一个错误

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

最佳答案

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

关于c# - 如何修复 "The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53675850/

有关c# - 如何修复 "The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time"错误的更多相关文章

随机推荐