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. 使用ResponseSelector实现校园招聘FAQ机器人

      本文主要介绍使用ResponseSelector实现校园招聘FAQ机器人,回答面试流程和面试结果查询的FAQ问题.FAQ机器人功能分为业务无关的功能和业务相关的功能2类. 一.data/nlu.y ...

  2. GPTs破冰硅基文明社会

    GPTs破冰硅基文明社会 渐进是技术革命的常态 技术革命看似一夕之间就颠覆了世界,但实际上每项重大技术进步的背后,都经历了漫长的渐进积累.以蒸汽机为例,最初动力微弱.效率低下,需要大量工程师跟车维护, ...

  3. 在ASP.NET Core 中使用 .NET Aspire 消息传递组件

    前言 云原生应用程序通常需要可扩展的消息传递解决方案,以提供消息队列.主题和订阅等功能..NET Aspire 组件简化了连接到各种消息传递提供程序(例如 Azure 服务总线)的过程.在本教程中,小 ...

  4. 怎么理解 Bean、Controller、Service、Servicelmpl、Mapper

    主要是对java的各层级介绍.叫法可能不一样,但实现的逻辑都差不多,扔给我的代码中是四个层,分别是Bean层.Controller层.Service层.Servicelmpl层.Mapper层. Be ...

  5. python数据类型元组、列表、集合、字典相互嵌套

    系统 Windows 10 专业工作站版22H2 软件 python-3.9.6-amd64.exe 拓展库: jupyter==1.0.0 notebook==7.0.6 1.元组嵌套 1.1 元组 ...

  6. [ABC246C] Coupon

    Problem Statement There are $N$ items in a shop. For each $i = 1, 2, \ldots, N$, the price of the $i ...

  7. CH395实现主动ping对端功能(代码及说明)

    目录 1.PING原理 1.1简介 1.2协议 1.3通信流程 2.代码解释 3.工程链接 PING原理 1.简介 PING是基于ICMP(Internet Control Message Proto ...

  8. 浅析 ArrayList

    by emanjusaka from https://www.emanjusaka.top/2023/12/java-arrayList 彼岸花开可奈何 本文欢迎分享与聚合,全文转载请留下原文地址. ...

  9. 【笔记整理】requests使用代理

    使用proxies参数传递代理信息 import requests if __name__ == '__main__': proxies = { # 这个字典的key不可以乱写,必须是http和htt ...

  10. 酷表ChatExcel -北大出品免费自动处理表格工具

    酷表ChatExcel是通过文字聊天实现Excel的交互控制的AI辅助工具,期望通过对表输入需求即可得到处理后的数据(想起来很棒),减少额外的操作,辅助相关工作人员(会计,教师等)更简单的工作.Cha ...