.Net Core: 跨域Cros概要
读取配置
public class AppConfig
{
public static IConfigurationRoot Configuration { get; set; } public static IConfigurationSection GetSection(string name)
{
return AppConfig.Configuration?.GetSection(name);
} public static T GetSection<T>(string name)
{
IConfigurationSection section = AppConfig.Configuration.GetSection(name);
if (section != null)
return section.Get<T>();
return default (T);
}
}
自定义配置
public class AccessPolicy
{
public string[] Origins { get; set; } public bool AllowAnyHeader { get; set; }//允许的头部 public bool AllowAnyMethod { get; set; }//允许的method:post\get…… public bool AllowAnyOrigin { get; set; }//允许所有origin public bool AllowCredentials { get; set; }//允许携带cookie等信息
}
Startup
services.AddCors();
……
AppConfig.Configuration = (IConfigurationRoot)Configuration;
app.UseCors(builder =>
{
var policy = AppConfig.GetSection<AccessPolicy>("AccessPolicy");
builder.WithOrigins(policy.Origins);
if (policy.AllowAnyHeader)
builder.AllowAnyHeader();
if (policy.AllowAnyMethod)
builder.AllowAnyMethod();
if (policy.AllowAnyOrigin)
builder.AllowAnyOrigin();
if (policy.AllowCredentials)
builder.AllowCredentials();
});
appsettings.json
"AccessPolicy": {
"Origins": [ "*" ],
"AllowAnyHeader": true,
"AllowAnyMethod": true,
"AllowAnyOrigin": true,
"AllowCredentials": true
}
资料
https://docs.microsoft.com/zh-cn/aspnet/core/security/cors?view=aspnetcore-2.2
.Net Core: 跨域Cros概要的更多相关文章
- 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET
让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...
- Asp.net Core 跨域配置
一般情况WebApi都是跨域请求,没有设置跨域一般会报以下错误 No 'Access-Control-Allow-Origin' header is present on the requested ...
- Asp.Net Core跨域配置
在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...
- Ajax跨域 CROS处理
Ajax跨域方法有多种 这里介绍CROS跨域的实际案例 场景:A域名 请求 B域名: 暂且 A为客户端 B为服务端: 请求的服务端必须自己能控制 或者服务器端头部已经添加 Access-Control ...
- 解决.Net Core跨域问题
什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 跨域的几种情况 1.端口和协议的不同,只能通过后台来解决 2.localhost和127.0.0.1虽然 ...
- Asp.net core 跨域设置
验证环境: dotnet core 2.1/Asp.net core2.1 一.作用域在中间件层 配置的方式是在startup.cs文件Configure(IApplicationBuilder a ...
- core跨域问题
#region 跨域问题 app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() ...
- .net core跨域设置
services.AddCors(options => options.AddPolicy("AllowSameDomain", builder => builder. ...
- 来吧学学.Net Core之登录认证与跨域资源使用
序言 学习core登录认证与跨域资源共享是越不过的砍,所以我在学习中同样也遇到啦这两个问题,今天我们就用示例来演示下使用下这2个技术点吧. 本篇主要内容如下: 1.展示一个登录认证的简单示例 2.跨域 ...
随机推荐
- ThinkPHP读取配置信息
use think\Config; dump(Config::get()); // 或者 dump(config());示例:dump(Config::get('database.database') ...
- springcloud断路器Dashboard监控仪表盘的使用
断路器Dashboard监控仪表盘的使用 在原有的orderserverfeignhystrix服务中使用 1.增加依赖仓库 <dependency> <g ...
- 在订单服务中使用Hystrix进行熔断设置
使用Hystrix熔断(上) 在一个分布式系统里,一个服务依赖多个服务,可能存在某个服务调用失败, 比如超时.异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败, ...
- Python--字典的一些用法dict.items()
1.dict.items() 例子1: 以列表返回可遍历的(键, 值) 元组数组. dict = {'Name': 'Runoob', 'Age': 7} print ("Value : % ...
- SysTick系统定时器(功能框图和优先级配置)
SysTick—系统定时器是属于 CM3 内核中的一个外设,内嵌在 NVIC 中.系统定时器是一个 24bit (2^24)的向下递减的计数器,计数器每计数一次的时间为 1/SYSCLK,一般我们设置 ...
- STM32固件库模板下载以及固件库学习方法
固件库模板下载 固件库模板新建过程: 下载我们上节的固件库文件 电脑新建一个文件夹命名为Fwlib-Template,在此文件夹下分别新建DOC Libraries Project User 这四个文 ...
- 一文搞懂嵌入式uboot、kernel、文件系统的关系
总览: 在linux系统软件架构可以分为4个层次(从低到高分别为): 1.引导加载程序 引导加载程序(Bootloader)是固化在硬件Flash中的一段引导代码,用于完成硬件的一 ...
- IE6/7下Select控件Display属性无效解决办法
HTML的Select控件,C#的DropDownList服务器控件 设置父类型Display属性之后,在IE6/7上无效 直接将下段javascript脚本添加到页面中即可 <script t ...
- JFinal(2)JFinal 学习资料
JFinal 学习资料 :http://pan.baidu.com/s/1hsOcQ0G 密码:7lq3 , jfinal社区收费会员内部部分资料,博主网络搜集而来
- Docker启动Mongo报警告WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
警告信息 2019-11-27T09:28:16.659+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_h ...