反伪造令牌Axios Asp .Net Core [英] Anti Forgery Token Axios Asp .Net Core

查看:208
本文介绍了反伪造令牌Axios Asp .Net Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用vue js axios将帖子发送到这样的Asp .NET Core 2.2中的控制器

Hi i am able to send a post using vue js axios to a controller in Asp .NET Core 2.2 like this

                axios({
                    url: '/Parametros/Create',
                    method: 'post',                    
                    ContentType: 'application/json',
                    data: formData                        
                })

无论如何,我必须从控制器的操作中删除

however for this to work i have to remove from my action in controller

[ValidateAntiForgeryToken]

此外,令牌在剃刀页面中作为输入生成

Also the token is generated in razor pages as input

<input name="__RequestVerificationToken" type="hidden" value="CfDJ8GwWLSmGzLVOqfs-yISjocyQshOjT98BeCqxo14sO91JGUZPe_IstyK9DWZyu0rCr0bxdx3lBlwminvxm7q0zXVWcUkAZIH8NwKDYGdNCiY-Z_BgMzLt_1PyNEHxfpmTouJgMu3il8N4fMjbI0ohwElXGK-eVLXGuzj_J5N_uQ3A4f-9ijmTKGk8p3BC7hrB1A">

我尝试过

axios({
    url: '/Parametros/Create',
    method: 'post',
    headers: { 
        "__RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val();
    }                    
    ContentType: 'application/json',
    data: formData                        
})

axios({
    url: '/Parametros/Create',
    method: 'post',                  
    ContentType: 'application/json',
    data: {
        "__RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val(),
        formData                        
    }
})

没有工作,我一直在提出不好的要求...使用ajax,第二种方法可以正常工作,但是axios不能.我该如何处理?

None work, I keep getting bad request... Using ajax the 2nd approach works fine but axios not. How can i handle this?

推荐答案

在标题中将__RequestVerificationToken更改为RequestVerificationToken使得发帖请求对我有用.

Changing __RequestVerificationToken to RequestVerificationToken in the header made a post request work for me.

最终代码:

         axios({
            method: 'post',
            headers: {  "RequestVerificationToken":   $('input[name="__RequestVerificationToken"]').val()},
            url:  'home/axiostest',
          })
...

这篇关于反伪造令牌Axios Asp .Net Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆