【问题标题】:ASP.NET MVC Ajax form submitted cross domain not sending Cookies跨域提交的 ASP.NET MVC Ajax 表单不发送 Cookie
【发布时间】:2020-01-11 18:55:11
【问题描述】:

我有一个网站,其中包含一些使用 ASP.NET MVC Ajax 的表单。 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">
}

请注意,由于 Url.Action 方法中的 Request.Url.Scheme 参数,该 URL 被设置为与浏览器从中获取该 URL 的域不同的域。这样做是因为主站点是使用 CDN 静态托管的,而表单是使用 AJAX 从另一个域加载的。这有效,除了 cookie 不在 AJAX 请求中发送。我试图通过使用OnBegin 事件和这个JavaScript 设置xhr.withCredentials = true 来发送cookie:

<script type="text/javascript">
    function SetWithCredentialsTrue(xhr) {
        console.log("SetWithCredentialsTrue(xhr)", xhr);
        xhr.withCredentials = true;
    }
</script>

虽然我可以看到 SetWithCredentialsTrue() 方法被调用,但它似乎不起作用,因为提交表单时生成的 HTTP 请求没有 Cookie 标头。

所有服务器端处理程序都将Access-Control-Allow-Credentials 响应标头设置为true,并将Access-Control-Allow-Origin 设置为主(静态)站点域。

更新:通过更多控制台日志记录,我已经验证传递给我的 OnBegin 事件处理程序 (SetWithCredentialsTrue) 的 xhr 参数不是 XMLHttpRequest 对象,因此设置 @987654322 @ 对它没有影响。 所以问题是如何访问 XMLHttpRequest 对象?

【问题讨论】:

    标签: asp.net-mvc cross-domain asp.net-ajax


    【解决方案1】:

    我终于想通了。 XMLHttpRequest 对象不通过 ASP.NET MVC 库公开。我能够更改 jquery.unobtrusive-ajax.js,这是 ASP.NET MVC 帮助程序使用的 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 是我添加的部分。

    【讨论】:

      【解决方案2】:

      根据我的理解,您想要做的是,您想使用 AJAX 帖子将数据从一个域发送到另一个域吗?

      如果是这样,我想告诉你,由于安全问题,浏览器不允许你这样做。

      如果您仍想这样做,那么您有两个选择,即使用 CORS 和 JSONP。

      http://csharp-video-tutorials.blogspot.in/2016/09/calling-aspnet-web-api-service-in-cross.html

      http://csharp-video-tutorials.blogspot.in/2016/09/cross-origin-resource-sharing-aspnet.html

      【讨论】:

      • 问题是关于将 ASP.NET MVC AJAX 表单从一个域提交到另一个域。我可以通过设置“xhr.withCredentials = true”使用 JQuery 发出类似的请求,但我想从 ASP.NET MVC Ajax 表单中做同样的事情。否则,CORS 正在工作 - 表单提交,它只是不发送 cookie 标头。
      猜你喜欢
      • 2014-07-16
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多