1.CORS中间件处理跨源请求。以下代码为具有指定源的整个应用程序启用CORS:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
} //跨域配置
string[] strs = { "https://localhost:44384" };
app.UseCors(builder => builder.WithOrigins(strs)); app.UseHttpsRedirection();
app.UseMvc(); }

2.跨域策略的定义

 public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //跨域设置
services.AddCors(Options =>
{ //①允许某一域名的请求
Options.AddPolicy("allow1", builder =>
{
builder.WithOrigins("https://localhost:44384");
}); //②允许所有域名的请求
Options.AddPolicy("allow2", builder =>
{
builder.AllowAnyOrigin();
}); //③允许某域名的请求,且限制该请求的类型
Options.AddPolicy("allow3", builder =>
{
builder.WithOrigins("https://localhost:44384").WithMethods("POST","HEAD");
//builder.WithOrigins("https://localhost:44384").AllowAnyMethod();//允许任何http请求的方法
}); //④允许某域名的请求,带请求头部信息
Options.AddPolicy("allow4", builder =>
{
builder.WithOrigins("https://localhost:44384").WithHeaders("accept", "content-type", "origin", "x-custom-header");
//builder.WithOrigins("https://localhost:44384").AllowAnyHeader();//
//builder.WithOrigins("https://localhost:44384").WithExposedHeaders("x-custom-header");//指定的说明文头信息
});
//⑤允许某域名的请求,预检时间多久可以被缓存
Options.AddPolicy("allow5", builder =>
{
builder.WithOrigins("https://localhost:44384").SetPreflightMaxAge(TimeSpan.FromSeconds(2520));
}); Options.AddPolicy("allow6", builder =>
{
builder.WithOrigins("https://localhost:44384");
});
});
}

开启其中一种策略

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseCors("allow5");
app.UseHttpsRedirection();
app.UseMvc();
}

在mvc层中开启策略,在Controller、Action头部添加相应策略名称

    [EnableCors("allow6")]
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
}

参考连接:https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1#cors-with-named-policy-and-middleware

.net core 下的跨域设置的更多相关文章

  1. IIS下配置跨域设置Access-Control-Allow-Origin

    设置Access-Control-Allow-Origin 打开IIS,找到“HTTP响应标头”点进去, 在右侧可以看到添加,然后添加如下标头即可 Access-Control-Allow-Heade ...

  2. Asp.net core 跨域设置

    验证环境: dotnet core 2.1/Asp.net core2.1 一.作用域在中间件层  配置的方式是在startup.cs文件Configure(IApplicationBuilder a ...

  3. Mac下的Chrome或Safari访问跨域设置,MBP上使用模拟器Simulator.app或iphone+Safari调试网页

    Mac下的Chrome或Safari访问跨域设置: mac下终端启动Chrome $ open -a Google\ Chrome --args --disable-web-security 或 /A ...

  4. chrome浏览器的跨域设置 Google Chrome浏览器下开启禁用缓存和js跨域限制--disable-web-security

    chrome用户默认路径 Win7:C:\Users\[用户名]\AppData\Local\Google\Chrome\User Data\XP:C:\Documents and Settings\ ...

  5. C# ASP.NET MVC/WebApi 或者 ASP.NET CORE 最简单高效的跨域设置

    概述 前面写了一篇:<C# ASP.NET WebApi 跨域设置>的文章,主要针对 ASP.NET WebApi 项目. 今天遇到 ASP.NET MVC 项目也需要设置跨域,否则浏览器 ...

  6. .NET CORE 3.1.5 跨域设置

    1.Startup配置 1 #region 跨域设置 2 //注意:放到services.AddMvc()之前 3 services.AddCors(options => { 4 options ...

  7. .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片

    .NET压缩图片保存   需求: 需要将用户后买的图片批量下载打包压缩,并且分不同的文件夹(因:购买了多个用户的图片情况) 文章中用到了一个第三方的类库,Nuget下载 SharpZipLib 目前用 ...

  8. asp.net (webapi) core 2.1 跨域配置

    原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...

  9. ASP.NET Core Web API 跨域(CORS) Cookie问题

    身为一个Web API,处理来自跨域不同源的请求,是一件十分合理的事情. 先上已有的文章,快速复制粘贴,启用CORS: Microsoft:启用 ASP.NET Core 中的跨域请求 (CORS) ...

随机推荐

  1. SpringBoot2.0拦截器 与 1.X版本拦截器 的实现

    1.5  版本 先写个拦截器,跟xml配置方式一样,然后将拦截器加入spring容器管理 .接着创建 配置文件类 继承 WebMvcConfigurerAdapter 类,重写父类方法addInter ...

  2. pip安装任何包都出现问题

    <!DOCTYPE html> { margin: 0; padding: 0; } body { background: url(images/body_bg.png) repeat-x ...

  3. c# 陈景润 15 子问题

    初学编程时在 csdn 上写过一个陈景润 15 子问题的项目,https://blog.csdn.net/weixin_41628344/article/details/79171846 当时的主要精 ...

  4. 使用Nuget重新安装packages.config中的组件的方法

    Update-Package -ProjectName 'Ko.app.web' -Reinstall 该语句作用:按照packages.config中给出的程序组件,重新下载安装一遍.

  5. 9、springcloud整合logback打印sql语句

    Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core.logback- classic和logback-access.logback-c ...

  6. ionic3.0 中带顶部导航的下拉刷新列表的实现

    1.最终实现效果 2.html代码布局: 3.css样式控制(注:下面这两个css类名需在浏览器解析后才可看到)

  7. 嵌入式 emmc 中 安装 烧录 内核 kernel,设备树 devicetree ,根文件系统 rootfs

    一般调试嵌入式开发板喜欢选择  利用 TFTP 传送  内核与 设备树,  利用 nfs 加载根文件系统. uboot 环境变量 设置如下: bootargs=root=/dev/nfs rw nfs ...

  8. 同一子网建立ssh通道,映射到本地

    在办公室有一台机器连入同一子网络,开启jupyter-notebook但是只能在这台机器上访问到,怎样可以在家也可以访问呢? 此时最简单的方法是在本地建立一个ssh通道: 在本地终端中输入 ssh u ...

  9. No symbol table is loaded. Use the "file" command.

    No symbol table is loaded.  Use the "file" command. gdb 1. 首先使用gcc   -g    .c文件   -o  可执行文 ...

  10. linux磁盘空间占用分析

    df -h # 查看目前磁盘空间占用 cd / # 切换到根目录 du -sh # 查询每个目录占用的大小 lsof | grep delete # 查看当前系统打开文件 # 删除不使用的文件, 如果 ...