【问题标题】:Kendo Upload - CSRF Token with ASP.NET剑道上传 - 带有 ASP.NET 的 CSRF 令牌
【发布时间】:2016-06-20 23:17:16
【问题描述】:

据我所见,Kendo 上传在 POST 请求正文中发送 CSRF 令牌。我想要做的是在向 Web API 方法发出请求时验证此令牌,但似乎无法弄清楚。

有人有什么想法吗?有什么我应该覆盖或更改的特定内容吗?

谢谢。

【问题讨论】:

  • 你用ValidateAntiFogeryToken属性装饰你的web api action方法了吗?
  • 是的。它对我不起作用。 Kendo 上传在 POST 请求正文中发送令牌,而不是在标头中。
  • 询问并answered。

标签: ajax asp.net-mvc asp.net-web-api kendo-ui kendo-upload


【解决方案1】:

see the anser

<meta name="_token" content="csrf_token()" />

<input type="file" name="files" id="photos" />

<script>
  var token = $('meta[name="_token"]').attr('content');  

$("#photos").kendoUpload({
async: {
    saveUrl: "http://url/save"
},
upload: onUpload
});

function onUpload(e) {
var xhr = e.XMLHttpRequest;
if (xhr) {
    xhr.addEventListener("readystatechange", function (e) {
        if (xhr.readyState == 1 /* OPENED */) {
            xhr.setRequestHeader("X-CSRF-TOKEN", token);
        }
    });
 }
}
</script>

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多