C# jquerypost&;ASP.NET MVC防伪令牌验证

C# jquerypost&;ASP.NET MVC防伪令牌验证,c#,c#-4.0,jquery,antiforgerytoken,jquery-post,C#,C# 4.0,Jquery,Antiforgerytoken,Jquery Post,在我的控制器中 [HttpPost] [OutputCache(CacheProfile = "Short", VaryByParam = "id", VaryByCustom = "none")] [ValidateAntiForgeryToken(Salt = @ApplicationEnvironment.SALT)] public ActionResult username(string id) { try {

在我的控制器中

  [HttpPost]
        [OutputCache(CacheProfile = "Short", VaryByParam = "id", VaryByCustom = "none")]
        [ValidateAntiForgeryToken(Salt = @ApplicationEnvironment.SALT)]
        public ActionResult username(string id) {
            try {
                if (id == null || id.Length < 3)
                    return Json(false, JsonRequestBehavior.AllowGet);

                var member = Membership.GetUser(id);

                if (member == null) {
                    //string userPattern = @"^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$";
                    string userPattern = "[A-Za-z][A-Za-z0-9._]{5,14}";
                    if (Regex.IsMatch(id, userPattern))
                        return Json(true, JsonRequestBehavior.AllowGet);
                }

            } catch (Exception ex) {
                CustomErrorHandling.HandleErrorByEmail(ex, "Validate LogName()");
                return Json(false, JsonRequestBehavior.AllowGet);
            }
            //found e false
            return Json(false, JsonRequestBehavior.AllowGet);

        }
[HttpPost]
[OutputCache(CacheProfile=“Short”,VaryByParam=“id”,VaryByCustom=“none”)]
[ValidateAntiForgeryToken(Salt=@ApplicationEnvironment.Salt)]
公共操作结果用户名(字符串id){
试一试{
if(id==null | | id.长度<3)
返回Json(false,JsonRequestBehavior.AllowGet);
var member=Membership.GetUser(id);
if(成员==null){
//字符串userPattern=@“^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$| ^[a-zA-Z]*[\S]$”;
字符串userPattern=“[A-Za-z][A-Za-z0-9.{5,14}”;
if(Regex.IsMatch(id,userPattern))
返回Json(true,JsonRequestBehavior.AllowGet);
}
}捕获(例外情况除外){
CustomErrorHandling.HandleErrorByMail(例如,“验证日志名()”);
返回Json(false,JsonRequestBehavior.AllowGet);
}
//发现e是假的
返回Json(false,JsonRequestBehavior.AllowGet);
}
在我看来,我这样做是为了post,我能够使用HTTPGET检索getJSON的数据。问题是,当用户在名称中添加空格时,它不会验证并给出404。所以我想我可以使用post方法来实现这一点,但似乎我无法发送我的日志名,因为我正在通过序列化发送数据中的反伪造令牌

有人能帮忙吗?我将不胜感激

@{
    string SALT = ApplicationEnvironment.SALT;
}

<script type="text/javascript" defer="defer">
    $(function () { 
        if($("#LogName").length > 0){

            $("#LogName").blur(function(){
                var logValidate = "/Validation/username/" + $("#LogName").val();
                //anti-forgery token works don't need to create the input field here but if I pass the logname into the form as an input then if a user puts double quote/single quote then it will also crash.. so is there any other way around. 
                var data = $('<form>@Html.AntiForgeryToken(SALT)</form>').serialize();
                $.post(logValidate, data, function (json) {
                    alert(json);
                });
            });
            $("#Email").blur(function(){
                var emailValidate = "/Validation/email/" + $("#Email").val();

                alert("got it");
            });
        }
    });
</script>
@{
字符串SALT=ApplicationEnvironment.SALT;
}
$(函数(){
如果($(“#LogName”).length>0){
$(“#LogName”).blur(函数(){
var logValidate=“/Validation/username/”+$(“#LogName”).val();
//anti-forgery token works不需要在这里创建输入字段,但是如果我将logname作为输入传递到表单中,那么如果用户输入双引号/单引号,那么它也会崩溃。还有其他方法吗。
var data=$('@Html.AntiForgeryToken(SALT)').serialize();
$.post(logValidate、数据、函数(json){
警报(json);
});
});
$(“#Email”).blur(函数(){
var emailValidate=“/Validation/email/”+$(“#email”).val();
警惕(“明白了”);
});
}
});

在发送请求时,如何通过jquery获取反伪造令牌的值? 生成反伪造密钥作为隐藏字段:

@Html.AntiForgeryToken()
查看源代码并调整“#uu请求验证打开”选择器


在发送请求时,通过jquery获取antiforgery令牌的值如何? 生成反伪造密钥作为隐藏字段:

@Html.AntiForgeryToken()
查看源代码并调整“#uu请求验证打开”选择器