Is it possible to access the username (and optionally password), currently used for HTTP authentication from Javascript code on the page?
Asked
Active
Viewed 2,777 times
2
-
only if its stored in cookies or is entered in login form ;) – c69 Sep 16 '11 at 12:04
1 Answers
1
Edit: In this answer I explain how to access a page or service via ajax that requires basic http authentication.
If you mean basic http authentication, then it should be possible (I have never tried it myself).
Basic authentication means that you add the http header Authorization
to your request, and the XmlHttpRequest
object supports adding custom headers with the method setRequestHeader()
.
The http header looks like this:
Authorization: Basic {authentication token}
where the authentication token is constructed like this:
authentication token := base64(username + ":" + password)
Javascript doesn't have standard functions to encode to Base64, but this SO question shows you how to do that.

Community
- 1
- 1

Elian Ebbing
- 18,779
- 5
- 48
- 56
-
1Thanks for the answer. What I meant is to access the credential used to retrieve the current HTML page. Like `$_SERVER['HTTP_AUTH_USER']` on the server-side. – vbence Sep 16 '11 at 12:33
-
@vbence - Well, then I don't think it is possible. There is no API available in javascript to get this information. – Elian Ebbing Sep 16 '11 at 14:32