• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
学习笔记
Misaka的学习笔记
博客园    首页    新随笔    联系   管理    订阅  订阅
.net core 配置跨域访问

在Startup类里面先配置ConfigureServices方法

     #region 跨域访问

     string[] coreslink = Configuration.GetSection("AppSetting:Cores").Value.Split(',');

     //services.AddCors(options =>
     // {
     //     options.AddPolicy("all", builder =>
     //     {
     //         builder.WithOrigins(coreslink) //允许特定来源的主机访问
     //         .AllowAnyMethod()
     //         .AllowAnyHeader()
     //         .AllowCredentials();
     //     });
     // });

     services.AddCors(options =>
           options.AddPolicy("all", builder =>
  {
      builder.AllowAnyMethod()//允许任何来源的主机访问
          .SetIsOriginAllowed(_ => true)
          .AllowAnyHeader()
          .AllowCredentials();
  })
);

     #endregion 跨域访问
builder.WithOrigins(coreslink) 是配置允许特定来源的主机访问

其次在Configure方法配置,跨域访问要放在

UseRouting和UseAuthorization之间
 app.UseRouting();

 #region 跨域访问

 app.UseCors("all");

 #endregion 跨域访问

 app.UseAuthorization();

 

posted on 2024-01-27 15:09  我们打工人  阅读(171)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3