.net core 下的跨域设置
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
{
}
.net core 下的跨域设置的更多相关文章
- IIS下配置跨域设置Access-Control-Allow-Origin
设置Access-Control-Allow-Origin 打开IIS,找到“HTTP响应标头”点进去, 在右侧可以看到添加,然后添加如下标头即可 Access-Control-Allow-Heade ...
- Asp.net core 跨域设置
验证环境: dotnet core 2.1/Asp.net core2.1 一.作用域在中间件层 配置的方式是在startup.cs文件Configure(IApplicationBuilder a ...
- Mac下的Chrome或Safari访问跨域设置,MBP上使用模拟器Simulator.app或iphone+Safari调试网页
Mac下的Chrome或Safari访问跨域设置: mac下终端启动Chrome $ open -a Google\ Chrome --args --disable-web-security 或 /A ...
- chrome浏览器的跨域设置 Google Chrome浏览器下开启禁用缓存和js跨域限制--disable-web-security
chrome用户默认路径 Win7:C:\Users\[用户名]\AppData\Local\Google\Chrome\User Data\XP:C:\Documents and Settings\ ...
- C# ASP.NET MVC/WebApi 或者 ASP.NET CORE 最简单高效的跨域设置
概述 前面写了一篇:<C# ASP.NET WebApi 跨域设置>的文章,主要针对 ASP.NET WebApi 项目. 今天遇到 ASP.NET MVC 项目也需要设置跨域,否则浏览器 ...
- .NET CORE 3.1.5 跨域设置
1.Startup配置 1 #region 跨域设置 2 //注意:放到services.AddMvc()之前 3 services.AddCors(options => { 4 options ...
- .NET压缩图片保存 .NET CORE WebApi Post跨域提交 C# Debug和release判断用法 tofixed方法 四舍五入 (function($){})(jQuery); 使用VUE+iView+.Net Core上传图片
.NET压缩图片保存 需求: 需要将用户后买的图片批量下载打包压缩,并且分不同的文件夹(因:购买了多个用户的图片情况) 文章中用到了一个第三方的类库,Nuget下载 SharpZipLib 目前用 ...
- asp.net (webapi) core 2.1 跨域配置
原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...
- ASP.NET Core Web API 跨域(CORS) Cookie问题
身为一个Web API,处理来自跨域不同源的请求,是一件十分合理的事情. 先上已有的文章,快速复制粘贴,启用CORS: Microsoft:启用 ASP.NET Core 中的跨域请求 (CORS) ...
随机推荐
- 什么是索引?Mysql目前主要的几种索引类型
一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...
- C# winform 文件管理
1.FolderBrowserDialog 打开文件夹中默认路径下的excl文件 private void button7_Click(object sender, EventArgs e) { Fo ...
- zabbix添加对web页面url的状态监控
zabbix3.0.4添加对web页面url的状态监控 1.应用集配置 在配置—>主机中打开主机列表,选择需要添加监控主机的web,创建应用集 2.web监测配置 选择web场景,再单击右上角的 ...
- connect failed: 127.0.0.1#953: connection refused
Problem1 : root@jeremy-VirtualBox:/etc/bind# /etc/init.d/bind9 restart * Stopping domain name servic ...
- CF1216X
由于rating限制,和慎老师用小号打了一场div 3 从A到F都没啥思维含量..感觉最难想的就是C了?? CF1216C 考完以后想hack一下这道题,然后发现满屏都是分类讨论 我大概是写不动 or ...
- mySQL单表限制大小
MySQL单表大小的限制在目前的技术环境中,由所在主机的OS上面的文件系统来界定而不是由MySQL数据库本身来决定了. 在老版本的MySQL 3.22中,MySQL单表大小为4GB,当时的MySQL的 ...
- cd 切换
切换
- leetcode-163周赛-1261-在污染的二叉树中查找元素
题目描述: 方法一: class FindElements: def __init__(self, root: TreeNode): self.d = set() def f(r, x): if r: ...
- java poi导出Excel合并单元格并设置边框
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; i ...
- img引用网络图片资源无法加载问题解决
近期在自己项目中遇到引用一些网络图片资源,显示无法加载,但是在浏览器打开图片路径又可以显示的问题 解决办法: 在图片显示的界面把meta referrer标签改为never <meta name ...