【发布时间】:2012-12-01 06:21:43
【问题描述】:
我们有一个受基本身份验证保护的网站,其下方有一个特定的 javascript 文件。
我想通过在加载文件时将详细信息放在身份验证标头上来从不同的 MVC3 站点加载该 javascript 文件,这就是我正在尝试的似乎不起作用的方法 - 它总是弹出基本身份验证登录对话框:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Auth test</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/Base64.js"></script>
<h2>Index</h2>
<script type="text/javascript">
$(document).ready(function () {
var auth = make_base_auth('domain\user', 'password');
$.ajaxSetup({
headers: {
'Authorization': auth
}
});
$.ajax({
url: 'https://localhost/WebClient/scripts/bob.js',
dataType: 'script',
//username: 'domain\user',
//password: 'password',
//withCredentials: true,
//crossDomain: true,
//headers: { 'Authorization': auth },
success: function (data) {
alert("Got the script!");
callFunctionInTheOtherJSFile();
},
async: false
});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
</script>
</body>
</html>
有什么想法可以让代码正常工作吗?
正如您从各种注释掉的部分中看到的那样 - 我尝试了几种不同的方法(新旧 jQuery 方法等)!
作为参考,Base64 JS 取自这里 - http://www.webtoolkit.info/javascript-base64.html 如此处所述 - http://coderseye.com/2007/how-to-do-http-basic-auth-in-ajax.html
【问题讨论】:
标签: javascript jquery asp.net-mvc-3 basic-authentication