<p>In a recent sharepoint project, I implemented an authentication webpart which should replace the NTLM authentication dialog box. It works fine as long as the user provides valid credentials. Whenever the user provides invalid credentials, the NTLM dialog box pops up in Internet Explorer.</p>

<p>My Javascript code which does the authentication via XmlHttpRequest looks like this:</p>

<pre><code>function Login() {
   var request = GetRequest(); // retrieves XmlHttpRequest
   request.onreadystatechange = function() {
      if (this.status == 401) {     // unauthorized request -&gt; invalid credentials
         // do something to suppress NTLM dialog box...
         // already tried location.reload(); and window.location = &lt;url to authentication form&gt;;
      }
   }
   request.open("GET", "http://myServer", false, "domain\\username", "password");
   request.send(null);
}
</code></pre>

<p>I don't want the NTLM dialog box to be displayed when the user provides invalid credentials. Instead the postback by the login button in the authentication form should be executed. In other words, the browser should not find out about my unauthorized request.</p>

<p>Is there any way to do this via Javascript?</p>

[{'answer_id': 29908, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 2, 'selected': False, 'text': "<p>IIRC, the browser pops the auth dialog when the following comes back in the request stream:</p>\n\n<ul>\n<li>Http status of 401</li>\n<li>WWW-Authenticate header</li>\n</ul>\n\n<p>I would guess that you'd need to suppress one or both of those. The easy way to do that is to have a login method that'll take a Base64 username and password (you are using HTTPS, right?) and return 200 with a valid/invalid status. Once the password has been validated, you can use it with XHR.</p>\n"}, {'answer_id': 165867, 'author': 'Jay', 'author_id': 20840, 'author_profile': 'https://Stackoverflow.com/users/20840', 'pm_score': 4, 'selected': True, 'text': '<p><a href="https://stackoverflow.com/users/2199/mark-brackett">Mark</a>\'s comment is correct; The NTLM auth prompt is triggered by a 401 response code and the presence of NTLM as the first mechanism offered in the WWW-Authenticate header (Ref: <a href="http://curl.haxx.se/rfc/ntlm.html" rel="nofollow noreferrer">The NTLM Authentication Protocol</a>).</p>\n<p>I\'m not sure if I understand the question description correctly, but I think you are trying to wrap the NTLM authentication for SharePoint, which means you don\'t have control over the server-side authentication protocol, correct? If you\'re not able to manipulate the server side to avoid sending a 401 response on failed credentials, then you will not be able to avoid this problem, because it\'s part of the (client-side) spec:</p>\n<h3><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/" rel="nofollow noreferrer">The XMLHttpRequest Object</a></h3>\n<blockquote>\n<p>If the UA supports HTTP Authentication [RFC2617] it SHOULD consider requests\noriginating from this object to be part of the protection space that includes the\naccessed URIs and send Authorization  headers and handle 401 Unauthorised requests\nappropriately. if authentication fails, UAs should prompt the users for credentials.</p>\n</blockquote>\n<p>So the spec actually calls for the browser to prompt the user accordingly if any 401 response is received in an XMLHttpRequest, just as if the user had accessed the URL directly. As far as I can tell the only way to really avoid this would be for you to have control over the server side and cause 401 Unauthorized responses to be avoided, as Mark mentioned.</p>\n<p>One last thought is that you may be able to get around this using a proxy, such a separate server side script on another webserver. That script then takes a user and pass parameter and checks the authentication, so that the user\'s browser isn\'t what\'s making the original HTTP request and therefore isn\'t receiving the 401 response that\'s causing the prompt. If you do it this way you can find out from your &quot;proxy&quot; script if it failed, and if so then prompt the user again until it succeeds. On a successful authentication event, you can simply fetch the HTTP request as you are now, since everything works if the credentials are correctly specified.</p>\n'}, {'answer_id': 17046512, 'author': 'Steve Lineberry', 'author_id': 443503, 'author_profile': 'https://Stackoverflow.com/users/443503', 'pm_score': 0, 'selected': False, 'text': '<p>I was able to get this working for all browsers except firefox.  See my blog post below from a few years ago.  My post is aimed at IE only but with some small code changes it should work in Chrome and safari.  </p>\n\n<p><a href="http://steve.thelineberrys.com/ntlm-login-with-anonymous-fallback-2/" rel="nofollow">http://steve.thelineberrys.com/ntlm-login-with-anonymous-fallback-2/</a></p>\n\n<p>EDIT:</p>\n\n<p>The gist of my post is wrapping your JS xml call in a try catch statement.  In IE, Chrome, and Safari, this will suppress the NTLM dialog box.  It does not seem to work as expected in firefox.  </p>\n'}]
['https://Stackoverflow.com/questions/29680', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/830/']