从.NET Core 2.2迁移到.NET Core 3.0时发生CORS问题

编程入门 行业动态 更新时间:2024-10-22 22:57:50
本文介绍了从.NET Core 2.2迁移到.NET Core 3.0时发生CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从 core 2.2迁移到 core 3.0时,我遇到了cors问题

I am getting cors issue while i am migrating from core 2.2 to core 3.0

public void Configure(IApplicationBuilder app, IHostEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime applicationLifetime) { NLog.LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog")); LogManager.Configuration.Variables["connectionString"] = Encryptor.GetNLogDB(); LogManager.Configuration.Variables["ApplicationName"] = env.ApplicationName; LogManager.Configuration.Variables["EnvironmentType"] = env.EnvironmentName; app.UseStaticFiles(); app.UseRouting(); app.UseCors("AllowAll"); applicationLifetime.ApplicationStopping.Register(OnShutdown); applicationLifetime.ApplicationStarted.Register(OnStarted); app.UseAuthentication(); app.UseAuthorization(); app.UseLogAndExceptionHandler(); //app.UseCors(); //app.UseSignalR(routes => //{ // routes.MapHub<ApplicationHub>(SignalRHub); // routes.MapHub<QuillHub>(QuillHub); //}); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); //app.UseMvc(); Initialize(app); } public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()); }); }

在运行此程序时,我收到一条错误消息,说我不能同时使用AllowAnyOrigin和AllowCredentials,并且当我删除其中的任何一条时,CORS策略已阻止了运行时错误:对预检请求的响应未通过访问控制检查:当请求的凭据模式为"include"时,响应中"Access-Control-Allow-Origin"标头的值不得为通配符"*".XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制.

While running this i am getting an error saying i cant use AllowAnyOrigin and AllowCredentials together and when i remove any of those i get run time error has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我想允许所有起源.

推荐答案

基于 CORS文档.

尝试使用 SetIsOriginAllowed 作为解决方法

services.AddCors(options => { options.AddPolicy("AllowAll", builder => builder.AllowAnyMethod() .AllowAnyHeader() .SetIsOriginAllowed(_ => true) .AllowCredentials()); });

更多推荐

从.NET Core 2.2迁移到.NET Core 3.0时发生CORS问题

本文发布于:2023-11-07 11:13:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生   Core   NET   CORS

发布评论

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

>www.elefans.com

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