• app.Development.json(开发环境),app.Production.json(生产环境)

    //跨域处理
    "withOrigins": "*",
    //如果用到websoket, 且需要跨域,需要配置对应的跨域前端地址
    "withOriginsValue": [
      "http://rg1008.com",
      "http://www.rg1008.com",
      "https://rg1008.com",
      "https://www.rg1008.com",
      "http://rg1008.cn",
      "http://www.rg1008.cn",
      "https://rg1008.cn",
      "https://www.rg1008.cn",
      "http://bt.sandieji.tech",
      "http://www.bt.sandieji.tech",
      "https://bt.sandieji.tech",
      "https://www.bt.sandieji.tech"
    ],
    
    //开启 websoket
    "websoket": {
      "open": true
    },
    //跨域处理
    "withOrigins": "*",
    //如果用到websoket, 且需要跨域,需要配置对应的跨域前端地址
    "withOriginsValue": [
      "http://localhost:5775",
      "http://www.localhost:5775",
      "http://localhost:5776",
      "http://www.localhost:5776",
      "http://localhost:5777",
      "http://www.localhost:5777",
      "http://localhost:5778",
      "http://www.localhost:5778"
    ],
  • Startup.cs 文件开启跨域规则

    `csharp
    //允许跨域
    string origins = _configuration.GetValue(“withOrigins”);

          List<string> originsValue = new List<string>();
          _configuration.GetSection("withOriginsValue").Bind(originsValue);

    app.UseCors(temp =>
    {

     if (originsValue != null && originsValue.Length > 0)
     {
         //webSocket 时候使用
         temp.WithOrigins(originsValue).SetIsOriginAllowed(origin => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
     }
     else if (origins == "*")
     {
         // .AllowCredentials().SetIsOriginAllowed(origin => true).AllowCredentials()
         temp.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
     }
     else
     {
         //限制运行特定域名跨域访问
         temp.WithOrigins(origins.Split(',')).AllowAnyMethod().AllowAnyHeader();
     }

    });

`

文档更新时间: 2021-06-24 08:00   作者:admin