C# 被CORS封锁

C# 被CORS封锁,c#,asp.net-core,C#,Asp.net Core,为什么当我在HTTP站点上时,CORS会阻止我,但不管这种配置如何,都会向HTTPS发送请求 我知道放置UseCors的顺序很重要,所以我尝试在用户外出之前和之后使用它,但这些都不起作用,为什么 第页我正在以下位置执行请求: http://localhost:5000/ 错误 阻止对不同来源的资源的请求:“同源策略”不允许从“”加载远程资源(CORS请求失败) 区别在于HTTPS和HTTP,但我允许吗 .NET核心3.1 public void ConfigureServices(IServic

为什么当我在
HTTP
站点上时,CORS会阻止我,但不管这种配置如何,都会向
HTTPS
发送请求

我知道放置UseCors的顺序很重要,所以我尝试在用户外出之前和之后使用它,但这些都不起作用,为什么

第页我正在以下位置执行请求:
http://localhost:5000/

错误

阻止对不同来源的资源的请求:“同源策略”不允许从“”加载远程资源(CORS请求失败)

区别在于
HTTPS
HTTP
,但我允许吗

.NET核心3.1

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<Context>(o => o.UseSqlServer(Configuration["Db:ConnectionString"]));

    services.AddTransient<IPasswordHasher<User>, PasswordHasher<User>>();

    services.AddAuthentication(...).AddJwt(...);

    services.AddCors();

    services.AddMvc();

    services.AddResponseCompression(opts =>
    {
        opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "application/octet-stream" });
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Context context)
{
    context.Database.EnsureCreated();

    app.UseResponseCompression();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBlazorDebugging();
    }

    app.UseStaticFiles();
    app.UseClientSideBlazorFiles<Client.Startup>();

    app.UseRouting();

    app.UseCors((x) =>
    {
        x
        .AllowAnyOrigin()
        .AllowAnyHeader()
        .AllowAnyMethod();
    });

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
        endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
    });
}
public void配置服务(IServiceCollection服务)
{
AddDbContext(o=>o.UseSqlServer(配置[“Db:ConnectionString]”);
services.AddTransient();
services.AddAuthentication(…).AddJwt(…);
services.AddCors();
services.AddMvc();
services.AddResponseCompression(选项=>
{
opts.MimeTypes=ResponseCompressionDefaults.MimeTypes.Concat(新[]{“应用程序/八位字节流”});
});
}
公共void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境、上下文)
{
context.Database.recreated();
app.UseResponseCompression();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBlazorDebugging();
}
app.UseStaticFiles();
app.UseClientSideBlazorFiles();
app.UseRouting();
应用程序UseCors((x)=>
{
x
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
app.UseEndpoints(端点=>
{
endpoints.MapDefaultControllerOute();
MapFallbackToClientSideBlazor(“index.html”);
});
}

目标方是否允许CORS?@HenkHolterman这是目标方的代码不同的端口表示不同的主机/来源。如果希望在两个端口上都可以访问它,则需要注册https和http处理程序。还要确保你没有例外。异常清除CORS听到(他们实际上清除了所有标题)。此外,您必须在路由中间件之前使用
UseCors
。中间件是按照注册的顺序执行的,如果您也想保护静态文件,那么您也必须将其放在静态文件中间件之前