如何解决"CORS协议不允许同时指定通配符(任何)起源和凭证".错误

编程入门 行业动态 更新时间:2024-10-27 16:30:07
本文介绍了如何解决"CORS协议不允许同时指定通配符(任何)起源和凭证".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I've already enabled CORS on the project in C# 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("example?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:

获取www.nuget/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.请参阅 docs.microsoft/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 docs.microsoft/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> ("example?prm=2"); } }

希望这对您有帮助...

Hope this helps...

更多推荐

如何解决"CORS协议不允许同时指定通配符(任何)起源和凭证".错误

本文发布于:2023-10-23 04:24:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   不允许   凭证   如何解决   起源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!