读取配置

  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. VC++单文档程序固定菜单栏和工具栏

    MainFrm.cpp框架类下,找到OnCreate方法 m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY); m_wndToolBar.EnableDocking( ...

  2. mac 安装 navicat for mysql 破解版

    mac 安装 navicat for mysql 破解版,直接安装,亲测可用 首先打开mac控制台输入命令行:sudo spctl --master-disable 百度盘,提取码: vrtr 失效请 ...

  3. docker 实践四:仓库管理

    本篇我们来了解 docker 仓库的内容. 注:环境为 CentOS7,docker 19.03 仓库(Responsitory)是集中存放镜像的地方,又分公共仓库和私有仓库. 注:有时候容易把仓库与 ...

  4. spring cloud微服务实践三

    上篇文章里我们实现了spring cloud中的服务提供者和使用者.接下来我们就来看看spring cloud中微服务的其他组件. 注:这一个系列的开发环境版本为 java1.8, spring bo ...

  5. SQL Server 2017命令创建新账户(test-user),并分配数据库权限

    -- 1. 创建登录账号USE [master];GOCREATE LOGIN [test-user] WITH PASSWORD = 'xysu7SZ193SNX6E{{HxubPE3}vr',DE ...

  6. Java Web 深入分析(1)B/S架构概述

    B/S结构即浏览器和服务器结构.它是随着Internet技术的兴起,对C/S结构的一种变化或者改进的结构.在这种结构下,用户工作界面是通过WWW浏览器来实现,极少部分事务逻辑在前端(Browser)实 ...

  7. Map集合中key不存在时使用toString()方法、valueOf()方法和强制转换((String))之间的区别

    1.toString()方法 底层代码 public String toString() { return this; } 其返回值为String类型的字符串本身 Map<String, Obj ...

  8. optparser模块 与 ZIP爆破(Python)

    optparser模块: 为脚本传递命令参数. 初始化: 带 Usage 选项(-h 的显示内容 Usage:): >>> from optparse import OptionPa ...

  9. docker系列七之Dockerfile

    Dockerfile 一. Dockerfile是什么   Dockerfile是docker中镜像文件的的描述文件,说的直白点就是镜像文件到底是由什么东西一步步构成的.例如我们在淘宝上买了一件商品, ...

  10. java计算接口调用时间

    方法一: LocalDateTime beginTime = LocalDateTime.now(); Long opetime = Duration.between(between,LocalDat ...