自从MVC4升级到MVC5以来,我注意到我的网页中添加了一个额外的服务器头:
X-Frame-Options:SAMEORIGIN
我理解添加此标记的安全性好处,但其中一个页面意味着包含在其他项目(在其他域上)的iframe中,这个额外的标头阻止了这一点.
我已经验证它不是主机IIS7服务器添加标头,当我降级回MVC4 - 标头消失了.
有谁知道如何从MVC5中删除此默认值?
我正在尝试对ASP.NET MVC控制器操作执行跨域POST请求.此控制器操作接受并使用各种参数.问题是,当预检请求发生时,控制器操作实际上会尝试执行,因为OPTIONS请求没有传递任何数据,控制器操作会抛出500 HTTP错误.如果我删除使用该参数的代码或参数本身,则整个请求链成功完成.
这是如何实现的一个例子:
控制器动作
public ActionResult GetData(string data)
{
return new JsonResult
{
Data = data.ToUpper(),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
Run Code Online (Sandbox Code Playgroud)
客户端代码
<script type="text/javascript">
$(function () {
$("#button-request").click(function () {
var ajaxConfig = {
dataType: "json",
url: "http://localhost:8100/host/getdata",
contentType: 'application/json',
data: JSON.stringify({ data: "A string of data" }),
type: "POST",
success: function (result) {
alert(result);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Error: Status: ' + textStatus + ', Message: ' + errorThrown);
}
};
$.ajax(ajaxConfig);
});
});
</script> …Run Code Online (Sandbox Code Playgroud) 我有一个MVC项目,其中有几个我想要公开跨域的JSON控制器方法.不是整个网站,只是这两种方法.
我基本上想要在这篇文章中为cors所说的确切内容:
http://enable-cors.org/server_aspnet.html
但是,问题是我有一个常规的MVC项目,而不是一个WEB API,这意味着,我不能按照注册寄存器的步骤
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors();
}
Run Code Online (Sandbox Code Playgroud)
因为它在我的MVC项目中不存在.
有没有办法使用这个库,虽然它是一个MVC项目?
我知道我可以通过web.config使用以下命令配置:
<httpProtocol>
<customHeaders>
<clear />
<add name="Access-Control-Allow-Origin" value="http://www.domain.com" />
</customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)
但我不想暴露所有方法,我想指定多个域(2个域)来访问我的方法......
我正在尝试创建一个在MVC 5中使用跨源请求(CORS)的Web应用程序.我已经尝试了所有内容而没有任何结果.
带有属性
public class AllowCrossSiteJsonAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
使用EnableCors属性
[EnableCors("*")]
Run Code Online (Sandbox Code Playgroud)
什么都行不通我开始认为这是不可能的
我在Chrome中遇到以下错误:
对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:9000 "因此不允许访问.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIISPlatformHandler();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseCors(policy => policy
.WithOrigins("http://localhost:9000")
.AllowAnyMethod()
.WithHeaders("Access-Control-Allow-Origin, Content-Type, x-xsrf-token, Authorization")
.AllowCredentials());
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
根据chrome,没有一个标题被添加到响应中.
access-control-allow-origin在Asp.NET 5 中将标题添加到选项响应的正确方法是什么?
我实际上正在努力用CORS发出POST请求.
我实际上是一个在我的控制器内部的方法上添加正确响应头的类
using System;
using System.Web.Mvc;
public class AllowCrossSiteAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4200");
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "*");
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true");
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
我就是这样用的
[AllowCrossSite]
public ActionResult GetReportParameters(string IdRapport)
Run Code Online (Sandbox Code Playgroud)
但问题是当我尝试使用自定义标头发出POST请求以传递此特定内容类型时
'Content-Type': 'application/json; charset=utf-8'
Run Code Online (Sandbox Code Playgroud)
我实际上得到了这个响应头
因此,即使我正确地进入属性类,也就像标题没有做任何事情一样.
这是我在Angular 2服务中的前端
const headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
const options = new RequestOptions({ headers: headers , withCredentials: true });
const mock = {
name: 'TitreRegroupement1',
visible: false,
type: 'System.String',
value: 'Test'
};
// tslint:disable-next-line:max-line-length
const …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用简单的HTML与JQuery页面和另一个域上的MVC站点进行简单的跨域调用.
我的基础是我做的...
在ASP.Net MVC中设置Access-Control-Allow-Origin - 最简单的方法
这是我简单网站中的电话......
<script type="text/javascript">
$(function () {
$.get("http://example.com:20874/Home/YourMethod", function (data) {
alert(data);
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
并且我的控制器......属性代码只是从其他问题粘贴...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[AllowCrossSiteJson]
public ActionResult YourMethod()
{
return Json(@"{""title"": ""example glossary""}");
}
}
Run Code Online (Sandbox Code Playgroud)
但调用网站错误...
XMLHttpRequest无法加载http://example.com:20874/Home/YourMethod.Access-Control-Allow-Origin不允许使用Origin http://example.com:90.
有人可以帮忙吗?
我正在尝试跨多个ASP.NET MVC Web应用程序进行共享身份验证.这些应用有不同的子域名,例如xxample.com,y.example.com.我已经生成了一个机器密钥并将其添加到了应用程序的web.config中.我已将域属性添加到forms标记(domain ="example.com").
这似乎在Chrome中正常运行.登录到第一个应用程序后,我导航到第二个应用程序,不需要登录.但是,在Firefox中,它似乎打破了登录.当我单击登录按钮时,Firefox似乎很快重新加载登录页面.我无法登录.
什么可能导致这种行为?是否有关于使用此域名编写的cookie,Firefox不喜欢?
我想将Mobile数字和EmailID使用 jQuery AJAX发送到我的 MVC 控制器,以检查这些详细信息是否已存在于数据库中。
当我[HttpPost]在我的操作方法上使用该属性时,出现以下错误:
跨域请求被阻止:同源策略不允许读取 url 上的远程资源。
如果我删除该[HttpPost]属性,则会调用我的 action 方法,但 action 方法参数中收到的所有值都是null.
下面是操作方法代码:
public class LogOnModel
{
public string Mobile { get; set; }
public string EmailID { get; set; }
}
[HttpPost]
[AllowAnonymous]
///AJAXService/LogOnAjax
public ActionResult LogOnAjax(LogOnModel obj)
{
return Json(true);
}
Run Code Online (Sandbox Code Playgroud)
下面是我的 AJAX 调用:
var LoginModel = {
'Mobile': "ASH",
'EmailID': "MITTAL",
};
$.ajax({
url: 'http://localhost:51603/AJAXService/LogOnAjax',
type: 'GET',
contentType: 'application/json',
dataType: 'jsonp',
data: JSON.stringify(LoginModel),
success: function …Run Code Online (Sandbox Code Playgroud) 对我来说似乎是一个挨打的主题,但我找不到答案.=(我从localhost:555应用程序使jquery ajax请求localhost:666
$.ajax({
url: "http://localhost:666/request",
dataType: 'json',
timeout: 5000,
success:...
Run Code Online (Sandbox Code Playgroud)
我有铬:
XMLHttpRequest无法加载http:// localhost:666/request.原产地的http://本地主机:555不被访问控制允许来源允许的.
这个问题的解决方案是什么?
asp.net-mvc ×10
cors ×5
asp.net ×3
c# ×3
jquery ×3
json ×2
ajax ×1
angular ×1
asp.net-core ×1
cross-domain ×1
firefox ×1
iis ×1
javascript ×1