<p>I have a few internal .net web application here that require users to "log out" of them. I know this may seem moot on an Intranet application, but nonetheless it is there.</p>

<p>We are using Windows authentication for our Intranet apps, so we tie in to our Active Directory with Basic Authentication and the credentials get stored in the browser cache, as opposed to a cookie when using .net forms authentication.</p>

<p>In IE6+ you can leverage a special JavaScript function they created by doing the following:</p>

<pre><code>document.execCommand("ClearAuthenticationCache", "false")
</code></pre>

<p>However, for the other browsers that are to be supported (namely Firefox at the moment, but I strive for multi-browser support), I simply display message to the user that they need to close their browser to log out of the application, which effectively flushes the application cache.</p>

<p>Does anybody know of some commands/hacks/etc. that I can use in other browsers to flush the authentication cache?</p>

[{'answer_id': 31334, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': -1, 'selected': False, 'text': '<p>Hopefully this will be useful until someone actually comes along with an explicit answer - <a href="http://forums.mozillazine.org/viewtopic.php?t=366028&amp;" rel="nofollow noreferrer"><strong>this issue was discussed two years ago on a message board</strong></a>.</p>\n\n<p>HTH</p>\n'}, {'answer_id': 31341, 'author': 'Andy', 'author_id': 1993, 'author_profile': 'https://Stackoverflow.com/users/1993', 'pm_score': 0, 'selected': False, 'text': '<p>Well, I\'ve been browsing around Bugzilla for a bit now and seemingly the best way you can go for clearing the authentication would be to send non-existant credentials.</p>\n\n<p>Read more here: <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=287957" rel="nofollow noreferrer">https://bugzilla.mozilla.org/show_bug.cgi?id=287957</a></p>\n'}, {'answer_id': 31385, 'author': 'Duncan Smart', 'author_id': 1278, 'author_profile': 'https://Stackoverflow.com/users/1278', 'pm_score': 2, 'selected': False, 'text': '<p>Why not use FormsAuth, but against ActiveDirectory instead as per the info in <a href="https://stackoverflow.com/questions/30861/authenticationg-domain-users-with-systemdirectoryservices">this thread</a>. It\'s just as (in)secure as Basic Auth, but logging out is simply a matter of blanking a cookie (or rather, calling <a href="http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.signout.aspx" rel="nofollow noreferrer">FormsAuthentication.SignOut</a>)</p>\n'}, {'answer_id': 1407111, 'author': 'Marius Marais', 'author_id': 114782, 'author_profile': 'https://Stackoverflow.com/users/114782', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve been searching for a similar solution and came across a patch for Trac (an issue management system) that does this.  </p>\n\n<p>I\'ve looked through the code (and I\'m tired, so I\'m not explaining everything); basically you need to do an AJAX call with <strong>guaranteed invalid</strong> credentials <strong>to your login page</strong>.  The browser will get a 401 and know it needs to ask you for the right credentials next time you go there.  You use AJAX instead of a redirect so that you can specify incorrect credentials and the browser doesn\'t popup a dialog.</p>\n\n<p>On the patch (<a href="http://trac-hacks.org/wiki/TrueHttpLogoutPatch" rel="nofollow noreferrer">http://trac-hacks.org/wiki/TrueHttpLogoutPatch</a>) page they use very rudimentary AJAX; something better like jQuery or Prototype, etc. is probably better, although this gets the job done.</p>\n'}, {'answer_id': 6289660, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 4, 'selected': False, 'text': '<p>I\'ve come up with a fix that seems fairly consistent but is hacky and <a href="https://stackoverflow.com/questions/6277919">I\'m still not happy with it</a>.</p>\n\n<p>It does work though :-)</p>\n\n<p>1) Redirect them to a Logoff page</p>\n\n<p>2) On that page fire a script to ajax load another page with dummy credentials (sample in jQuery):</p>\n\n<pre><code>$j.ajax({\n    url: \'&lt;%:Url.Action("LogOff401", new { id = random })%&gt;\',\n    type: \'POST\',\n    username: \'&lt;%:random%&gt;\',\n    password: \'&lt;%:random%&gt;\',\n    success: function () { alert(\'logged off\'); }\n});\n</code></pre>\n\n<p>3) That should always return 401 the first time (to force the new credentials to be passed) and then only accept the dummy credentials (sample in MVC):</p>\n\n<pre><code>[AcceptVerbs(HttpVerbs.Post)]\npublic ActionResult LogOff401(string id)\n{\n    // if we\'ve been passed HTTP authorisation\n    string httpAuth = this.Request.Headers["Authorization"];\n    if (!string.IsNullOrEmpty(httpAuth) &amp;&amp;\n        httpAuth.StartsWith("basic", StringComparison.OrdinalIgnoreCase))\n    {\n        // build the string we expect - don\'t allow regular users to pass\n        byte[] enc = Encoding.UTF8.GetBytes(id + \':\' + id);\n        string expected = "basic " + Convert.ToBase64String(enc);\n\n        if (string.Equals(httpAuth, expected, StringComparison.OrdinalIgnoreCase))\n        {\n            return Content("You are logged out.");\n        }\n    }\n\n    // return a request for an HTTP basic auth token, this will cause XmlHttp to pass the new header\n    this.Response.StatusCode = 401; \n    this.Response.StatusDescription = "Unauthorized";\n    this.Response.AppendHeader("WWW-Authenticate", "basic realm=\\"My Realm\\""); \n\n    return Content("Force AJAX component to sent header");\n}\n</code></pre>\n\n<p>4) Now the random string credentials have been accepted and cached by the browser instead. When they visit another page it will try to use them, fail, and then prompt for the right ones.</p>\n'}, {'answer_id': 8497804, 'author': 'AnthonyVO', 'author_id': 438458, 'author_profile': 'https://Stackoverflow.com/users/438458', 'pm_score': 3, 'selected': False, 'text': '<p>A couple of notes.  A few people have said that you need to fire off a ajax request with invalid credentials to get the browser to drop it\'s own credentials.   </p>\n\n<p>This is true but as Keith pointed out, it is essential that the server page claims to accept these credentials for this method to work consistently.</p>\n\n<p>On a similar note:  It is NOT good enough for your page to just bring up the login dialog via a 401 error.  If the user cancels out of the dialog then their cached credentials are also unaffected.</p>\n\n<p>Also if you can please poke MOZILLA at <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=287957" rel="noreferrer">https://bugzilla.mozilla.org/show_bug.cgi?id=287957</a> to add a proper fix for FireFox.  A webkit bug was logged at <a href="https://bugs.webkit.org/show_bug.cgi?id=44823" rel="noreferrer">https://bugs.webkit.org/show_bug.cgi?id=44823</a>.  IE implements a poor but functional solution with the method: </p>\n\n<pre><code>document.execCommand("ClearAuthenticationCache", "false");\n</code></pre>\n\n<p>It is unfortunate that we need to go to these lengths just to log out a user.</p>\n'}, {'answer_id': 9443989, 'author': 'staromeste', 'author_id': 680094, 'author_profile': 'https://Stackoverflow.com/users/680094', 'pm_score': 3, 'selected': False, 'text': '<p>Mozilla implemented the crypto object, available via the DOM <code>window</code> object, which has the <code>logout</code> function (Firefox 1.5 upward) to clear the SSL session state at the browser level so that "the next private operation on any token will require the user password again" (see <a href="https://developer.mozilla.org/en/JavaScript_crypto">this</a>).</p>\n\n<p>The crypto object seems to be an implementation of the <a href="http://html5.creation.net/webcrypto-api/">Web Crypto API</a>, and according to <a href="https://wiki.mozilla.org/Privacy/Features/DOMCryptAPISpec/Latest">this document</a>, the DOMCrypt API will add even more functions.</p>\n\n<p>As stated above Microsoft IE (6 upward) has:\n<code>document.execCommand("ClearAuthenticationCache", "false")</code></p>\n\n<p>I have found no way of clearing the SLL cache in Chrome (see <a href="http://code.google.com/p/chromium/issues/detail?id=90676">this</a> and <a href="http://code.google.com/p/chromium/issues/detail?id=90454">this</a> bug reports).</p>\n\n<p>In case the browser does not offer any API to do this, I think the better we can do is to instruct the user to close the browser.</p>\n\n<p>Here\'s what I do:</p>\n\n<pre><code>var agt=navigator.userAgent.toLowerCase();\nif (agt.indexOf("msie") !== -1) {\n    document.execCommand("ClearAuthenticationCache","false");\n}\n//window.crypto is defined in Chrome, but it has no logout function\nelse if (window.crypto &amp;&amp; typeof window.crypto.logout === "function"){\n    window.crypto.logout();\n}\nelse{\n    window.location = "/page/to/instruct/the/user/to/close/the/browser";\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31326', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/71/']