Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  app.UseCorsMiddleware();
//其它代码......
}
CorsMiddleware.cs
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http; namespace VipSoft.Web
{
public class CorsMiddleware
{
private readonly RequestDelegate _next; public CorsMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{ if (httpContext.Request.Method == "OPTIONS")
{
httpContext.Response.Headers.Add("Access-Control-Allow-Origin", httpContext.Request.Headers["Origin"]);
httpContext.Response.Headers.Add("Access-Control-Allow-Headers", httpContext.Request.Headers["Access-Control-Request-Headers"]);
httpContext.Response.Headers.Add("Access-Control-Allow-Methods", httpContext.Request.Headers["Access-Control-Request-Method"]);
httpContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
httpContext.Response.Headers.Add("Access-Control-Max-Age", "86400");//缓存一天
httpContext.Response.StatusCode = 204; #【用 AspNetCoreRateLimit 的时候,这行代码要去掉,否则会报:StatusCode cannot be set because the response has already started.】
return httpContext.Response.WriteAsync("");
}
if (httpContext.Request.Headers["Origin"] != "")
{
httpContext.Response.Headers.Add("Access-Control-Allow-Origin", httpContext.Request.Headers["Origin"]);
} httpContext.Response.Headers.Add("Access-Control-Allow-Headers", httpContext.Request.Headers["Access-Control-Request-Headers"]);
httpContext.Response.Headers.Add("Access-Control-Allow-Methods", httpContext.Request.Headers["Access-Control-Request-Method"]);
httpContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
httpContext.Response.Headers.Add("Access-Control-Max-Age", "86400");//缓存一天
return _next.Invoke(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CorsMiddlewareExtensions
{
public static IApplicationBuilder UseCorsMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CorsMiddleware>();
}
}
}

.Net Core 跨域的更多相关文章

  1. Asp.net Core 跨域配置

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

  2. Asp.Net Core跨域配置

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

  3. 解决.Net Core跨域问题

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

  4. Asp.net core 跨域设置

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

  5. core跨域问题

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

  6. .Net Core: 跨域Cros概要

    读取配置 public class AppConfig { public static IConfigurationRoot Configuration { get; set; } public st ...

  7. .net core跨域设置

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

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

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

  9. .net core 的跨域

    .net core 的跨域问题花了 我很长时间 接下来我简单的描述下解决过程 首先我在前端用jquery的ajax去调用自己的本地接口大致如下 $.ajax({ type:"POST&quo ...

  10. .net core入门-跨域访问配置

    Asp.net Core 跨域配置   一般情况WebApi都是跨域请求,没有设置跨域一般会报以下错误 No 'Access-Control-Allow-Origin' header is prese ...

随机推荐

  1. 第1章 .NET起步

    1.1 什么是.NET? .NET 8.0 SDK下载地址:https://dotnet.microsoft.com/zh-cn/download/dotnet/8.0 .NET 是一个免费的跨平台开 ...

  2. CSP2023-S复盘

    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path s ...

  3. R!!C!!E!!

    打开页面只有一句话提示,提示有敏感信息泄露 这里的话使用dirresearch扫描网站结构 在这里我们可以看到响应码正常且有反应的多数都是.git文件 这里我们使用Git源码下载的脚本下载Git源码 ...

  4. Chinese Bank Card and Credit Card ID

    Regular match expression: [^0-9]((3|4|5|6|9)\d{15,18})[^0-9] Rule characteristics: first number:3 or ...

  5. L2-034 口罩发放

    重点在阅读理解能力 身份证号必须是18位数字,话说平时咱们身份证也可以带个X啥的啊. 合法状态就是只要身份证合格就行,一开始我还想是它犯病,然后申请了口罩才算一个合法记录. 后边输出状态为1的人,一定 ...

  6. Jayway JsonPath-提取JSON文档内容的Java DSL

    介绍 JsonPath是一种能够提取部分JSON文档属性.对象.数组的语法,支持条件过滤.数学运算.字符串处理等功能.JsonPath与JSON文档就像 XPath 表达式与 XML 文档结合使用一样 ...

  7. ubuntu20 安装 mysql5.7.31 , 卸载mysql 8.0, Mysql只能本地登录,无法远程登录

    ubuntu 18 可以直接命令安装:# 安装mysql服务sudo apt-get install mysql-server# 安装客户端sudo apt install mysql-client# ...

  8. Bash—source命令&export命令&bashrc文件

    当不使用 source 命令执行脚本时,会创建一个子 shell,在该子 shell 中执行完脚本后退出子 shell.不是用 export 定义的变量只对该 shell 有效,对子 shell 是无 ...

  9. 数据库同步工具,PanguSync后起之秀

    随着数字化时代的快速发展,数据已经成为企业运营的核心.为了确保数据的准确性和一致性,数据库同步工具成为了企业不可或缺的工具.而在众多数据库同步工具中,PanguSync以其卓越的性能和强大的功能,逐渐 ...

  10. SpringBoot发送虚拟请求~

    1.创建一个测试用的TestController @RestController public class TestController { @GetMapping("/test" ...