在我的控制器中,
[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);
}
在我看来,我这样做是为了发布,我能够使用 HTTP Get 检索 getJSON 的数据。问题是,当用户在名称中添加一个空格时,它不会验证并给出 404。所以我认为我可以使用 post 方法执行此操作,但似乎我无法发送我的登录名,因为我是通过序列化在数据中发送 Antiforgery 令牌...
有人可以帮忙吗?我将感激不尽。
@{
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>