我一直在努力解决这个问题。 我有以下文档路径和锚标记
<a href="@link.DocumentPath" onclick="return checkAuth(@link.Id, @PasswordProtected)">@link.Title</a>
如果&#34; checkAuth&#34;我不想处理href。函数返回false。 这是&#34; checkAuth&#34;码。
var checkAuth = function(id, PasswordProtected) {
var result = null;
var hostname = location.hostname;
var host = '@System.Configuration.ConfigurationManager.AppSettings["hostroot"]';
if (hostname == "localhost")
host = "";
if (PasswordProtected == "1"){
var pass = prompt("This document is password protected", "");
var response = $.ajax({
type: "GET",
url: host + "/Communities/DeCryptPwd/",
data: {"id": id, "password": pass},
success: function (data) {
alert(data);
if (data == "True")
result = true;
if (data == "False")
result = false;
},
error: function (errorData) { alert(errorData); }
});
}
我只是不知道如何停止处理href上的文档并返回true ... continue进程,或者false - 停止处理。
万一你需要它,这里是&#34;服务器端&#34;由.ajax
调用的代码public bool DeCryptPwd(int id, string password) {
var encrypted = db.CommunityDocs.Where(x => x.Id == id).Select(x => x.Password).SingleOrDefault();
/* Extract the bytes */
byte[] hashBytes = Convert.FromBase64String(encrypted);
/* Get the salt */
byte[] salt = new byte[16];
Array.Copy(hashBytes, 0, salt, 0, 16);
/* Compute the hash on the password the user entered */
var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 10000);
byte[] hash = pbkdf2.GetBytes(20);
/* Compare the results */
for (int i = 0; i < 20; i++)
if (hashBytes[i + 16] != hash[i])
return false;
return true;
}
答案 0 :(得分:0)
您无法从包含异步函数的函数返回异步函数的结果。因此,您需要更改成功部分中的位置,如果不受保护,则返回true。
然而,代码目前允许他们右键单击并在新窗口中打开,无论
function checkAuth = function(theLink) {
var id = theLink.id,
protected = theLink.getAttribute("pass") == "1",
href = theLink.href,
result = null,
hostname = location.hostname,
host = '@System.Configuration.ConfigurationManager.AppSettings["hostroot"]';
if (hostname == "localhost")
host = "";
if (protected) {
var pass = prompt("This document is password protected", "");
var response = $.ajax({
type: "GET",
url: host + "/Communities/DeCryptPwd/",
data: {
"id": id,
"password": pass
},
success: function(data) {
alert(data);
if (data == "True") {
location = href;
}
},
error: function(errorData) {
alert(errorData);
}
});
return !protected; // return false if protected
}
<a href="@link.DocumentPath" onclick="return checkAuth(this)" id="@link.Id" data-pass="@PasswordProtected">@link.Title</a>