1. 程式人生 > >asp.net core設定跨域

asp.net core設定跨域

   public void ConfigureServices(IServiceCollection services)
        { var urls = AppConfigurtaionServices.Configuration.GetSection("Cors")["default"].Split(',');
            services.AddCors(options =>
            options.AddPolicy("AllowSameDomain",
            builder => builder.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin().AllowCredentials())
            );
        }

appsetting中加入   

"Cors": {

    "default": "http://localhost:24154/"

  }

允許跨域訪問的api控制器上面加上[EnableCors("AllowSameDomain")]這個特性即可

需要引用:Microsoft.AspNetCore.Cors