从 javascript 获取 HTTP 基本身份验证用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3460299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:13:45  来源:igfitidea点击:

Get the HTTP Basic Auth username from javascript?

javascripthttpbasic-authentication

提问by Rory

I have a webpage that is only accessible over http basic auth. How can I figure out the basic auth username in javascript in that page. i.e. when someone visits it (after logging in), I want to make a popup that says "Hello, you're currently signed in as the user $USERNAME"

我有一个只能通过 http 基本身份验证访问的网页。如何在该页面中的 javascript 中找出基本身份验证用户名。即当有人访问它时(登录后),我想制作一个弹出窗口,上面写着“您好,您目前以用户 $USERNAME 登录”

回答by bobince

It's not possible purely from JavaScript. You'd have to have the server side script pick up the username from the request, and insert it in the generated page code for the script to pick up. (eg. <script type="text/javascript">var username=(insert JSON-encoded string here);</script>.

仅靠 JavaScript 是不可能的。您必须让服务器端脚本从请求中提取用户名,并将其插入生成的页面代码中以便脚本提取。(例如<script type="text/javascript">var username=(在此处插入 JSON 编码的字符串);</script>

回答by tc.

javascript:window.alert(document.location)seems to give a URL with a username (but no password). This may not be true in other browsers.

javascript:window.alert(document.location)似乎给出了一个带有用户名(但没有密码)的 URL。这在其他浏览器中可能并非如此。