-1

When I access a site requiring authentication, the browser shows the login popup. How can I access the username and password entered there from jQuery? I'm not talking about fields in a web page form, but the popup generated by the browser.

user2647567
  • 37
  • 1
  • 7
  • Do you have access to the window.open method? – Sampgun Jun 30 '17 at 16:36
  • You mean basic-authentication & the browsers built-in dialog? If so you can't, you need to write it back to the page from the server (usually its in the server variables collection) – Alex K. Jun 30 '17 at 16:36
  • 2
    Why would you need to access this in the client? Once you've authenticated to the server, isn't that good enough? – Barmar Jun 30 '17 at 16:40
  • Currently, the credentials authorization string is hard-coded in the javascript. I need to get that out of the page. – user2647567 Jun 30 '17 at 17:53

1 Answers1

0

You cannot access this directly. As a work around you can create a page on the website that allows anonymous authentication so the dialog isn't triggered and then use jQuery on that page to collect a username and password and perform the authentication for you.

As described here How to use Basic Auth with jQuery and AJAX?:

jQuery (1.7.2+)

$.ajax
({
  type: "GET",
  url: "index1.php",
  dataType: 'json',
  async: false,
  username: username,
  password: password,
  data: '{ "comment" }',
  success: function (){
    alert('Thanks for your comment!'); 
  }
});

Or, pre 1.7.2 use headers instead of username and password

$.ajax
({
  type: "GET",
  url: "index1.php",
  dataType: 'json',
  async: false,
  headers: {
    "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
  },
  data: '{ "comment" }',
  success: function (){
    alert('Thanks for your comment!'); 
  }
});
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41