.NET WebAPI 跨域问题(has been blocked by CORS policy:No Access-Control-Allow-Ogigin header is present on the requested resource)
一、什么是跨域
1. 跨域解释
跨域指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。
同源指的是:域名,协议,端口均相同。
2. 什么情况下会导致跨域
2.1 不同域名(无论主域名还是子域名)
www.dennis.com 访问 www.dong.com
2.2 不同端口号
www.dennis.com 访问 www.dennis.com:8080
2.3 不同协议
http://www.dennis.com 访问 https://www.dennis.com
2.4 本地指向
localhost 访问 127.0.0.1
二、如何解决跨域
1. .NET 版本
1.1 NuGet包安装Microsoft.AspNet.Cors
搜索Microsoft.AspNet.Cors,下载并安装

1.2 配置 WebApiConfig
打开APP_Start文件夹下的 WebApiConfig,最后面加上 config.EnableCors(); 即可
当然这个有重载,支持固定的域名访问和固定的请求方式,可以自行测试
第一个参数是过滤域名的,多个用英文逗号隔开
第二个参数是过滤header的,没测试过,有兴趣自行测试
第三个参数是过滤请求方式的,多个用英文逗号隔开
如:config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 允许所有域名,所有请求方式的方法
如:config.EnableCors(new EnableCorsAttribute("https://www.dennis.com", "*", "get,post")); 允许www.dennis.com域名,所有get和post请求方式的方法
2. .NET Core 版本
2.1 配置Startup
Startup中声明全局私有常量
/// <summary>
/// 跨域配置名称
/// </summary>
private const string DefaultCorsPolicyName = "AllowCross";
在ConfigureServices方法中添加
services.AddCors(options =>
{
options.AddPolicy(DefaultCorsPolicyName, builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
});
在Configure方法中的app.UseRouting();//路由后面添加
app.UseCors(DefaultCorsPolicyName);//跨域
.NET WebAPI 跨域问题(has been blocked by CORS policy:No Access-Control-Allow-Ogigin header is present on the requested resource)的更多相关文章
- 前后端分离 导致的 静态页面 加载 <script type="module" > 报CORS 跨域错误,提示 blocked by CORS policy
1.前言 静态页面 加载 <script type="module" > 报CORS 跨域错误,提示Access to script at ftp:///xxx.js ...
- Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...
- As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource
默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...
- 解决跨域No 'Access-Control-Allow-Origin' header is present on the requested resource.
用angular发起http.get(),访问后端web API要数据,结果chrome报错:跨域了 Access to XMLHttpRequest at 'http://127.0.0.1:300 ...
- .Net Core 处理跨域问题Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
网页请求报错: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Or ...
- js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource
js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...
- (转)AJax跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource
在本地用ajax跨域访问请求时报错: No 'Access-Control-Allow-Origin' header is present on the requested resource. Ori ...
- ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)
问题 在某域名下使用Ajax向另一个域名下的页面请求数据,会遇到跨域问题.另一个域名必须在response中添加 Access-Control-Allow-Origin 的header,才能让前者成功 ...
- java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)
1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...
- 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'
NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...
随机推荐
- 2022春每日一题:Day 28
题目:最大上升子序列和 就是最长上升子序列的改版,贡献由1改为a[i]其他全部不变 代码: #include <cstdio> #include <cstdlib> #incl ...
- Java对象拷贝原理剖析及最佳实践
作者:宁海翔 1 前言 对象拷贝,是我们在开发过程中,绕不开的过程,既存在于Po.Dto.Do.Vo各个表现层数据的转换,也存在于系统交互如序列化.反序列化. Java对象拷贝分为深拷贝和浅拷贝,目前 ...
- MyEclipse反编译插件安装于使用
在MyEclipse开发中,使用反编译插件可以对jar包的源码进行随机的查看,节约了使用jd-gui查看时间. 百度云分享地址:链接:https://pan.baidu.com/s/1efNR6A 密 ...
- 数电第二周总结_by_yc
数电第二周总结_CC 重点: 模块实例化.仿真测试.数值表示.参数.表达式. 模块实例化端口连接方法: A.顺序端口连接:需严格按照模块定义时的顺序 B.明明端口连接:对端口信号顺序不做要求 Ex-1 ...
- JavaEE Day05 JDBC(用Java语言操作数据库)
今日内容 基本概念 快速入门 对JDBC中各个接口和类的详解 一.基本概念 1.概念:Java Database Connectivity:Java数据库连接,Java语言操作数据库 2.本质:官方( ...
- 【离线数仓】Day03-系统业务数据仓库:数仓表概念、搭建、数据导入、数据可视化、Azkaban全调度、拉链表的使用
一.电商业务与数据结构简介 1.业务流程 2.常识:SKU/SPU SKU=Stock Keeping Unit(库存量基本单位).现在已经被引申为产品统一编号的简称,每种产品均对应有唯一的SKU号. ...
- 你真的了解 RSA 加密算法吗?
作者:小傅哥 博客:https://bugstack.cn 源码:https://github.com/fuzhengwei/java-algorithms 沉淀.分享.成长,让自己和他人都能有所收获 ...
- Linux 常用命令(持续更新)
Linux常用命令介绍(备查) *所有的命名都可以用 命令 --help/man 命令 查看使用说明 1.pwd 显示当前路径 2.dir 和 ls用法一样 都是列出当前路径下的文件(不包括隐藏文件) ...
- 安装es客户端软件elasticsearch-head
安装ElasticSearch插件 一 Head插件介绍 elasticsearch-head是elasticsearch的一款可视化工具,依赖于node.js ,所以需要先安装node.js 二 安 ...
- http转成https工具类
工具类代码如下: 点击查看代码 package com.astronaut.auction.modules.oss.utils; import org.apache.commons.collectio ...