ASP.NET Core跨域设置
项目中经常会遇到跨域问题,解决方法:
在appsettings.json 文件中添加json项
{"Logging": {"LogLevel": {"Default": "Warning"}},"AllowedHosts": "*","AppCores": "https://www.xxx0.com,https://www.xxx1.com" }
在Startup文件中进行设置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SignalRTestCore.SignalR;namespace SignalRTestCore
{public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){services.Configure<CookiePolicyOptions>(options =>{// This lambda determines whether user consent for non-essential cookies is needed for a given request.options.CheckConsentNeeded = context => true;options.MinimumSameSitePolicy = SameSiteMode.None;}); var urls = Configuration["AppCores"].Split(',');services.AddCors(options => options.AddPolicy("AllowAll",policy => policy.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowCredentials()));services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler("/Error");// 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.UseHttpsRedirection();app.UseStaticFiles();app.UseCookiePolicy();app.UseCors("AllowAll");app.UseMvc();}}
}
在Controller类上进行跨域设置 [EnableCors("AllowAll")]
转载于:https://www.cnblogs.com/zengxia/p/11143601.html
ASP.NET Core跨域设置相关推荐
- ASP.NETAPI跨域设置(超简易)
跨域设置 只需要在[Web.config]的 <system.webServer>标签内添加如下编码即可: <httpProtocol><customHeaders> ...
- 【SpringMVC】与权限拦截器冲突导致的Cors跨域设置失效问题
问题描述 前端域名FE.com向后端域名BE.com分别请求访问优惠券的列表和提交新增的优惠券,API设计所用的Method分别为Get和Post,结果为前一次访问成功而后一次访问失败.这两次请求都是 ...
- chrome浏览器的跨域设置 Google Chrome浏览器下开启禁用缓存和js跨域限制--disable-web-security...
chrome用户默认路径 Win7:C:\Users\[用户名]\AppData\Local\Google\Chrome\User Data\ XP:C:\Documents and Settings ...
- 浏览器允许跨域设置(不用于生产环境,开发用)
Firefox 之前看过FF下关闭跨域限制的方法: firefox安全性强,不允许跨域调用.Firefox 要取消XMLHttpRequest的跨域限制的话 从 about:config 里设置 si ...
- yii2 跨域请求配置_伸手党系列四:vuecli3.0以上 使用 proxy 进行跨域设置
跨域:出于浏览器的同源策略限制,当一个请求url的协议.域名.端口三者之间任意一个与当前页面url不同即为跨域. process:process 对象是一个全局变量,它提供有关当前 Node.js 进 ...
- data 谷歌浏览器更改user 路径_chrome浏览器的跨域设置——包括版本49前后两种设置...
做前后分离的webapp开发的时候,出于一些原因往往需要将浏览器设置成支持跨域的模式,好在chrome浏览器就是支持可跨域的设置,网上也有很多chrome跨域设置教程.但是新版本的chrome浏览器提 ...
- SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析
最近在研究SSO单点登录技术,其中有一种就是通过js的跨域设置cookie来达到单点登录目的的,下面就已京东商城为例来解释下跨域设置cookie的过程 涉及的关键知识点: 1.jQuery ajax跨 ...
- php跨域单点登录,SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析...
SSO单点登录技术,其中有一种就是通过js的跨域设置cookie来达到单点登录目的的,下面就已京东商城为例来解释下跨域设置cookie的过程 涉及的关键知识点: 1. 跨域redirect实例: te ...
- chrome浏览器的跨域设置,前端修改跨域问题
原文:https://www.cnblogs.com/laden666666/p/5544572.html 做前后分离的webapp开发的时候,出于一些原因往往需要将浏览器设置成支持跨域的模式,好在c ...
最新文章
- Leangoo看板工具做单团队敏捷开发
- java getRuntime().exec 带符号的命令 无法执行 解决方法
- 人工智能不是计算机领域能学么,人工智能的几个热门领域简介及作用
- arthas 查看哪个方法调用最耗时_Arthas实战
- VS2005混合编译ARM汇编代码
- Java 10及更高版本的思考
- Chrome调试WebView时Inspect出现空白的解决方法(使用离线包不Fan墙)
- leetcode129. 求根到叶子节点数字之和(dfs)
- 域名解析中的cname解析和显性URL跳转和隐性URL跳转三者有什么区别
- 谷歌云端语音识别助手手机版-Google云端语音识别app1.0.0 官方安卓版-东坡下载...
- 无法Debug SQL: Unable to start T-SQL Debugging. Could not attach to SQL Server process on
- asp毕业设计——基于asp+access的网上音乐网站设计与实现(毕业论文+程序源码)——网上音乐网站
- 关于下载Keil5无法打开keil4文件的问题解决方案
- 抛弃clover,爱上QTTabBar
- 两个网卡做映射linux,linux多网卡的路由模式和桥接模式设置方法
- Windows下的IIS日志管理
- swiper+vue3,使用自动切换autoplay+自定义分页器navigation的报错问题解决
- 腾讯位置服务---->(小程序简单使用+显示附近WC步行路线)
- Dbeaver报错:The server time zone value ‘etd‘ is unrecognized
- 大学最后一个学习的半年,Do crazily without hesitation