【问题标题】:How to dynamically load a javascript file from a server with Basic Authentication如何使用基本身份验证从服务器动态加载 javascript 文件
【发布时间】: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


    【解决方案1】:

    您是否尝试过标准的http://user:pass@hostname/ 语法?

    【讨论】:

    • 是的——那也没用。虽然这可能是因为我还需要添加用户的域 - 即domain\user:pass@hostname/
    • 请注意,如果有任何区别,网址也在 https 下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多