Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net .NET Core 2.0中的CORS“;否';访问控制允许原点';请求的资源上存在标头。”;_Asp.net_Asp.net Core_Cors - Fatal编程技术网

Asp.net .NET Core 2.0中的CORS“;否';访问控制允许原点';请求的资源上存在标头。”;

Asp.net .NET Core 2.0中的CORS“;否';访问控制允许原点';请求的资源上存在标头。”;,asp.net,asp.net-core,cors,Asp.net,Asp.net Core,Cors,我已经使用Core 2.0创建了一个web api,当执行启用cors的跨域调用时,收到以下错误:“请求的资源上不存在“Access Control Allow Origin”头。” 下面是我在startup.cs中的配置: public void ConfigureServices(IServiceCollection services) { services.AddLogging(); services.AddMvc(); se

我已经使用Core 2.0创建了一个web api,当执行启用cors的跨域调用时,收到以下错误:“请求的资源上不存在“Access Control Allow Origin”头。”

下面是我在startup.cs中的配置:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging();
        services.AddMvc();
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll",
                builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                });
        });

        services.Configure<MvcOptions>(options =>
        {
            options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAll"));
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        app.UseMvc();
        app.UseCors("AllowAll");    
    }
public void配置服务(IServiceCollection服务)
{
services.AddLogging();
services.AddMvc();
services.AddCors(选项=>
{
options.AddPolicy(“AllowAll”,
生成器=>
{
建设者
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
配置(选项=>
{
options.Filters.Add(新的科索授权过滤器工厂(“AllowAll”);
});
}
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
app.UseMvc();
附录UseCors(“AllowAll”);
}

根据我在stackoverflow或其他地方看到的每一篇文章/教程,这是允许所有来源的正确方法。我错过了什么?

试着打电话:
app.UseCors(“AllowAll”)

缔约国声明如下:

要为整个应用程序启用CORS,请使用UseCors扩展方法将CORS中间件添加到请求管道中。请注意,CORS中间件必须位于应用程序中要支持跨源请求的任何已定义端点之前(例如,在调用UseMvc之前)


我可能错了,但本教程应该可以帮助您

下面是startup.cs中的配置

public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();
        services.AddDbContext<DataContext>(x => x.UseInMemoryDatabase("TestDb"));
        services.AddMvc();
        services.AddAutoMapper();

        // configure strongly typed settings objects
        var appSettingsSection = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingsSection);

        // configure jwt authentication
        var appSettings = appSettingsSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);
        services.AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(x =>
        {
            x.RequireHttpsMetadata = false;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = false,
                ValidateAudience = false
            };
        });

        // configure DI for application services
        services.AddScoped<IUserService, UserService>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        // global cors policy
        app.UseCors(x => x
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());

        app.UseAuthentication();

        app.UseMvc();
    }
public void配置服务(IServiceCollection服务)
{
services.AddCors();
AddDbContext(x=>x.UseInMemoryDatabase(“TestDb”);
services.AddMvc();
services.AddAutoMapper();
//配置强类型设置对象
var appsetingssection=Configuration.GetSection(“AppSettings”);
services.Configure(应用设置部分);
//配置jwt身份验证
var appSettings=appSettingsSection.Get();
var key=Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x=>
{
x、 DefaultAuthenticateScheme=JwtBearerDefaults.AuthenticationScheme;
x、 DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x=>
{
x、 RequireHttpsMetadata=false;
x、 SaveToken=true;
x、 TokenValidationParameters=新的TokenValidationParameters
{
ValidateSuersigningKey=true,
IssuerSigningKey=新对称性安全密钥(密钥),
validateisuer=false,
ValidateAudience=false
};
});
//为应用程序服务配置DI
services.addScope();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
//全球cors政策
app.UseCors(x=>x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseAuthentication();
app.UseMvc();
}