简体   繁体   English

无法使用反应前端解决此 CORS 问题

[英]Unable to resolve this CORS issue with a react frontend

I have read multiple solutions but somehow none worked for me.我已经阅读了多种解决方案,但不知何故没有一个对我有用。 I am still getting CORS error.我仍然收到 CORS 错误。 I am using a react front end and tried all solutions related to the frontend.我正在使用反应前端并尝试了与前端相关的所有解决方案。 I think my problem has to do with the backend .NET我认为我的问题与后端 .NET 有关

this is how my controller CORS settings look like.这就是我的 controller CORS 设置的样子。 I tried the wild card but it does not work either, I am now trying with the frontend address and port and still having CORS issues.我尝试了通配符,但它也不起作用,我现在正在尝试使用前端地址和端口,但仍然存在 CORS 问题。 any suggestion?有什么建议吗?

    [EnableCors(origins: ALLOWED_CORS_ORIGINS, headers: "*", methods: "*", SupportsCredentials = true)]
    public class TroubleshootingDataController : ApiController
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private const string ALLOWED_CORS_ORIGINS = "http://localhost:3000/";

startUp.cs启动.cs


[assembly: OwinStartup(typeof(WebAPI.Startup))]

namespace WebAPI
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            if (!Debugger.IsAttached)
            {
                //ConfigureAuth(app);
                GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnection", new SqlServerStorageOptions { SchemaName = "HangfireConnLiving" });
                var filter = new BasicAuthAuthorizationFilter(
                    new BasicAuthAuthorizationFilterOptions
                    {
                        RequireSsl = true,
                        LoginCaseSensitive = true,
                        Users = new[]
                        {
                            new BasicAuthAuthorizationUser
                            {
                                Login = "",
                                PasswordClear = ""
                            }
                        }
                    });
                app.UseHangfireDashboard("/hangfire", new DashboardOptions
                {
                    Authorization = new[]
                    {
                        filter
                    }
                });
                app.UseHangfireServer();
            }
        }
    }
}

On the backend on the Startup.cs just add this在 Startup.cs 的后端添加这个

On configure.在配置。

public void Configure(IApplicationBuilder appBuilder, IWebHostEnvironment webHostEnvironment)
{
    appBuilder.UseCors(); // this line must before the appBuilder.UseEndPoints();
}

On ConfigureServices, we configure the cors在 ConfigureServices 上,我们配置 cors

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddDefaultPolicy(builder =>
        {
            builder.AllowAnyOrigin();
            builder.AllowAnyHeader();
            builder.AllowAnyMethod();
        });
    });
}

Edit:编辑:

It seams you are using a diferent framework or something like that, that I'm used to work with.它使您正在使用我习惯使用的不同框架或类似的东西。

try to add this尝试添加这个

app.UseCors(CorsOptions.AllowAll);

before the if (.Debugger.IsAttached)if (.Debugger.IsAttached)之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM