.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) ...
随机推荐
- UVA10118_Free Candies状态压缩
这题大概题意是,有四列糖果,一个人手中最多拿五个水果,每次拿水果只能从每一列最上面开始拿. 而如果手中的糖果相同就会成对抵消,奖励给玩家 问玩家怎样取能取到最多的糖果,并输出对数 这题是运用动态规划, ...
- Data structure alignment by binary operation
在寫C的過程中,我們會很自然地以為,我連續宣告一堆大小不一的char array. 經過Complier之後這些char array未必是連續擺放.至於為什麼就要談到我們今天的主角了alignment ...
- docker报错: x509: certificate has expired or is not yet valid
环境:最小化安装centos7 问题:docker 启动没问题,但是查询 镜像时报错 Error response from daemon: Get https://index.docker.io/v ...
- maven工程的下载及其环境配置
Maven是一个项目管理工具,它给我们提供了好多有用的组件和工具. Maven下载: Maven下载载地址:http://maven.apache.org/download.cgi (1)进入下载界面 ...
- 使用TPL取回Task中的运行结果的三种方式
概念:TPL( Task Parallel Library) 任务并行库 使用Task类执行多线程操作要比直接使用自己手工创建Thread效率高很多. 默认情况下,TPL使用线程池中的线程执行Task ...
- ionic3 图片(轮播)预览 ionic-gallary-modal组件使用方法
一.效果展示 使用方法: 1.npm安装ionic-gallary-modal扩展模块 npm install ionic-gallery-modal --save 2.在app.module.ts根 ...
- shell同时输出多行信息
- tar - tar 档案文件管理程序的 GNU 版本。
总览 tar [ - ] A --catenate --concatenate | c --create | d --diff --compare | r --append | t --list | ...
- 在Eclipse上安装Spring Tool Suite
. 不装IDE会没有Spring bean configure file Spring Tool Suite是一个基于Eclipse IDE开发环境中的用于开发Spring应用程序的工具,提供了开箱即 ...
- Table边框合并
<style> table, table tr th, table tr td { border: 1px solid #0094ff; } table { width: 200px; m ...