Asp.net mvc ASP.NET MVC Ajax表单跨域提交,不发送cookie
我有一个网站,它有一些使用ASP.NETMVCAJAX的表单。Asp.net mvc ASP.NET MVC Ajax表单跨域提交,不发送cookie,asp.net-mvc,cross-domain,asp.net-ajax,Asp.net Mvc,Cross Domain,Asp.net Ajax,我有一个网站,它有一些使用ASP.NETMVCAJAX的表单。BeginForm方法示例: @using (Ajax.BeginForm("HandleSignin", "Profile", null, new AjaxOptions() { HttpMethod = "POST", Url = Url.Action("HandleSignin", "Profile", null, Request.Url.Scheme), OnBegin = "SetW
BeginForm
方法示例:
@using (Ajax.BeginForm("HandleSignin", "Profile", null, new AjaxOptions() {
HttpMethod = "POST",
Url = Url.Action("HandleSignin", "Profile", null, Request.Url.Scheme),
OnBegin = "SetWithCredentialsTrue(xhr)",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "signin-form-container" },
new { id = "sign-in-form", @class = "text-left-desktop group" }))
{
@Html.AntiForgeryToken()
@Html.TextBoxFor(x => Model.Email, new { placeholder = "Email" })
@Html.PasswordFor(x => Model.Password, new { placeholder = "Password" })
<input type="submit" value="SignIn" class="button small-button">
}
虽然我可以看到调用了SetWithCredentialsTrue()
方法,但在提交表单时生成的HTTP请求没有Cookie头的情况下,它似乎不起作用
所有服务器端处理程序都将Access Control Allow Credentials
响应头设置为true
,将Access Control Allow Origin
设置为主(静态)站点域
更新:通过更多的控制台日志记录,我已经验证了传递给OnBegin事件处理程序(
SetWithCredentialsTrue
)的xhr
参数不是对象,因此对它的设置没有影响。所以问题是如何访问XMLHttpRequest对象?据我所知,您想做的是,使用AJAX post将数据从一个域发送到另一个域,对吗
如果这是真的,我想告诉你们,由于安全问题,浏览器不允许你们这么做
如果您仍然想这样做,那么通过使用CORS和JSONP,您有两个选择
据我所知,您想做的是使用AJAX post将数据从一个域发送到另一个域,对吗 如果这是真的,我想告诉你们,由于安全问题,浏览器不允许你们这么做 如果您仍然想这样做,那么通过使用CORS和JSONP,您有两个选择
我终于明白了这一点。XMLHttpRequest对象未通过ASP.NET MVC库公开。我能够修改jquery.unobtrusive-ajax.js,这是ASP.NET MVC helper使用的js库,以便将withCredentials设置为true:
$(document).on("submit", "form[data-ajax=true]", function (evt) {
var clickInfo = $(this).data(data_click) || [],
clickTarget = $(this).data(data_target),
isCancel = clickTarget && clickTarget.hasClass("cancel");
evt.preventDefault();
if (!isCancel && !validate(this)) {
return;
}
asyncRequest(this, {
url: this.action,
type: this.method || "GET",
data: clickInfo.concat($(this).serializeArray()),
xhrFields: {
withCredentials: true
}
});
});
注意:xhrFields是我添加的部分。我终于解决了这个问题。XMLHttpRequest对象未通过ASP.NET MVC库公开。我能够修改jquery.unobtrusive-ajax.js,这是ASP.NET MVC helper使用的js库,以便将withCredentials设置为true:
$(document).on("submit", "form[data-ajax=true]", function (evt) {
var clickInfo = $(this).data(data_click) || [],
clickTarget = $(this).data(data_target),
isCancel = clickTarget && clickTarget.hasClass("cancel");
evt.preventDefault();
if (!isCancel && !validate(this)) {
return;
}
asyncRequest(this, {
url: this.action,
type: this.method || "GET",
data: clickInfo.concat($(this).serializeArray()),
xhrFields: {
withCredentials: true
}
});
});
注意:xhrFields是我添加的部分。问题是关于从一个域向另一个域提交ASP.NET MVC AJAX表单。通过设置“xhr.withCredentials=true”,我可以使用JQuery发出类似的请求,但我想从ASP.NET MVC Ajax表单中执行同样的操作。否则,CORS正在工作-表单提交,它只是不发送cookie头。问题是如何将ASP.NET MVC AJAX表单从一个域提交到另一个域。通过设置“xhr.withCredentials=true”,我可以使用JQuery发出类似的请求,但我想从ASP.NET MVC Ajax表单中执行同样的操作。否则,CORS正在工作-表单提交,它只是不发送cookie头。