Get the HTTP Basic Auth username from javascript?

我的梦境 提交于 2019-12-18 15:47:41

问题


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"


回答1:


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>.




回答2:


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



来源:https://stackoverflow.com/questions/3460299/get-the-http-basic-auth-username-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!