读取配置

  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概要的更多相关文章

  1. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET

    让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...

  2. Asp.net Core 跨域配置

    一般情况WebApi都是跨域请求,没有设置跨域一般会报以下错误 No 'Access-Control-Allow-Origin' header is present on the requested ...

  3. Asp.Net Core跨域配置

    在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...

  4. Ajax跨域 CROS处理

    Ajax跨域方法有多种 这里介绍CROS跨域的实际案例 场景:A域名 请求 B域名: 暂且 A为客户端 B为服务端: 请求的服务端必须自己能控制 或者服务器端头部已经添加 Access-Control ...

  5. 解决.Net Core跨域问题

    什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 跨域的几种情况 1.端口和协议的不同,只能通过后台来解决 2.localhost和127.0.0.1虽然 ...

  6. Asp.net core 跨域设置

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

  7. core跨域问题

    #region 跨域问题 app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() ...

  8. .net core跨域设置

    services.AddCors(options => options.AddPolicy("AllowSameDomain", builder => builder. ...

  9. 来吧学学.Net Core之登录认证与跨域资源使用

    序言 学习core登录认证与跨域资源共享是越不过的砍,所以我在学习中同样也遇到啦这两个问题,今天我们就用示例来演示下使用下这2个技术点吧. 本篇主要内容如下: 1.展示一个登录认证的简单示例 2.跨域 ...

随机推荐

  1. Mysql主从复制原理及同步延迟问题

    本文转载自:Mysql主从复制原理及同步延迟问题 主从复制解决的问题 数据分布:通过复制将数据分布到不同地理位置 负载均衡:读写分离以及将读负载到多台从库 备份:可作为实时备份 高可用性:利用主主复制 ...

  2. MySQL忘记密码解决

    1.设置管理员root密码为123 开启MySQL服务后 PS C:\WINDOWS\system32> mysqladmin -uroot -p password "123" ...

  3. go 结构的方法2

    你可以对包中的 任意 类型定义任意方法,而不仅仅是针对结构体. 但是,不能对来自其他包的类型或基础类型定义方法. package main import (     "fmt"   ...

  4. go 包的概念

    ------------------------------------------------------------------ package main import ( "fmt&q ...

  5. zookeeper-data

    1. The ZooKeeper Data Model 1.1 ZNodes Znodes maintain a stat structure: The Stat structure for each ...

  6. angular轮播图

    还是直接上代码比较好 <!doctype html><html lang="en"><head> <meta charset=" ...

  7. 根目录/缺少执行权限x产生的两种错误

    Linux根目录缺少x权限,产生的两个错误: 以root用户执行systemctl命令报权限相关问题 [root@hps2 ~]# systemctl stop hps-manager * (pktt ...

  8. C# 直播录制视频

    //项目引用 ffmpeg.exe 下载地址http://ffmpeg.org/ var time = DateTime.Now; ; //录制分钟 var fileName = Guid.NewGu ...

  9. 判断一个ip地址是动态的还是静态的

    要确定计算机的IP是静态IP还是动态IP,请执行以下步骤: 通过单击开始打开命令提示符并搜索CMD,然后单击cmd.exe 键入ipconfig / all.  找到“以太网本地连接”列表.找到“ I ...

  10. Apache Maven setting.xml

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...